2015-05-30 30 views
0

我嘗試使用Rails 3.2.21練習#163 Self-Referential Association和精細差不多的工作,但我只能在URL地址欄中顯示如何從user_path(:current)獲取「http:// localhost:3000/users/current」?

http://localhost:3000/users/1

而不是

http://localhost:3000/users/current

。我閱讀和搜索幾乎所有的代碼,我只找到一個current

<%= link_to "View Profile", user_path(:current) %>

我該怎麼辦?這是相對於Rails 3?非常感謝!

回答

2

的Railscast是有點標新立異,但user_path()方法,將採取一個值(對象,字符串,整數,符號),並在其上調用to_param。一個符號(在這種情況下:當前)將變成「當前」,並且構建的URL將是「/ users/current」。如果你傳遞一個@user(用戶實例),to_param方法將返回一個「1」(對象的id)給你「/ users/1」。

我說這個代碼是非常規的,因爲users_controller#show方法不使用ID來查找@user。它只是爲了方便將其設置爲current_user。瑞恩並沒有試圖教這個必要的東西,更多的是關於寧靜的友誼控制器和數據建模的友誼。

+0

謝謝!我忘了'to_param'的方法 – abelard2008

0

試試這個

<%= link_to "View Profile", users_path(:current) %>. 
+0

@@ Nitin我得到了「http:// localhost:5500/users.current」並且沒有顯示「http:// localhost:5500/users/1」的內容 – abelard2008

+0

無法獲取您的問題 –

+0

'users_path'方法不需要參數。如果有的話,它的格式(通常爲零,但可以是.html,.js,.xml等) – Swards

相關問題