i'm working on rails project giving me problems. i've got controller characters_controller.rb
has 2 methods.
class characterscontroller < applicationcontroller before_action :authenticate_player! def view @character = character.find(params[:id]) unless @character.player_id == current_player.id redirect_to :root end end def new end end
i've got routes set each of those.
get 'characters/:id', to: 'characters#view' 'characters/new', to: 'characters#new'
the first route works fine. can go /characters/1 , i'm shown appropriate view , requested information. if visit /characters/new i'm shown error references characters#view.
raise recordnotfound, "couldn't find #{name} '#{primary_key}'=#{id}"
and
app/controllers/characters_controller.rb:6:in `view'
so /characters/new trying character database id of "new" doesn't work well. idea may doing wrong?
order matters in routes.rb, router find first route matches.
in case, never go characters#new
, because line above match.
a simple solution swap 2 lines.
a better solution might use resource routing documented in rails routing guide.
Comments
Post a Comment