2014-03-27 34 views
3

我有一個相當特殊的問題。這是它涉及到routes.rb中的一部分:routes.rb - 不支持控制器名稱

resources :players 
match '/players/:userid', :to => 'Players#show' 

當您訪問本地主機:3000 /播放/ 1234它給這個錯誤:

'Players' is not a supported controller name. This can lead to potential routing problems.

控制器中的相關代碼:

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 

問題並沒有在那裏結束。當頁面加載時(它有時會隨機工作),這條代碼行動起來: if Player.find_by(:uid => :userid) then 使用PostgreSQL,查詢得到顯示。而不是使用URL中的:userid值(即localhost:3000/players/1234將是1234),它只是輸入文本「userid」。

我錯過了一些明顯的東西嗎?

真的很感謝任何幫助。

回答

8

變化

match '/players/:userid', :to => 'Players#show' 

match '/players/:userid', :to => 'players#show' 

Read more

要讀取控制器中的用戶標識值,請使用params[:userid],而不是:userid

+1

更好的仍然是使用:id而不是:userid,以更輕鬆的重定向(例如redirect_to @player,link_to「...」,@player) –

+0

我不能在這種情況下使用:id。 :userid指的是與不同服務關聯的帳戶的ID(我沒有處理登錄憑證)。如果我可以使用:id,我同意,我會。 – Nick

+0

你的意思是:在你的模型中使用uid,:userid只是一個參數,你可以把它命名爲:manchesterunited –