I'm having quite a peculiar issue. This is the portion of routes.rb it relates to:
resources :players
match '/players/:userid', :to => 'Players#show'
When you visit localhost:3000/players/1234 it gives this error:
'Players' is not a supported controller name. This can lead to potential routing problems.
The related code in the controller:
def show
begin
if Player.find_by(:uid => :userid) then
@playerattributes = Player.find_by(:uid => :userid)
if player[:profile_complete] == true then
@playerinfo = {
:age => player[:age],
:team => player[:team],
:position => player[:position]
}
else
@playerinfo = 0
end
end
player = Player.find_by(:uid => :userid)[:info]
rescue ActiveRecord::RecordNotFound
redirect_to root_url
end
end
The problem doesn't end there. When the page does load (which it sometimes randomly works), this line acts up:
if Player.find_by(:uid => :userid) then
Using PostgreSQL, and the query gets displayed. Instead of using the :userid value from the URL (i.e. localhost:3000/players/1234 would be 1234), it just inputs the text "userid".
Am I missing something obvious?
Really appreciate any help.
Change: Players to players so:
match '/players/:userid', :to => 'Players#show'
to
match '/players/:userid', :to => 'players#show'
Read more.
To read user id value in your controller, use params[:userid]
, not just :userid
.
Rails individual routes is going to write in this way.
EG: requested action "browser url_name(whatever you want)", to:
'controller_name#method_name'
In your case it should be:
match '/players/:userid', :to => 'players#show'
Similarly for a get request:
get '/players/:userid', :to => 'players#show'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With