在Rails的指導下2.5奇異資源,它指出掩蓋Rails3中奇異路線:ID
Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action.
所以,我想這個例子:
match "profile" => "users#show"
然而,當我試圖去到profile_path,它嘗試重定向到以下,其中id =:id:
/profile.id
這表示兩個問題:
- 我不想在所有顯示的ID,並認爲這是一個路由模式來掩蓋一個id
- 使用此方法使下面的錯誤。當我嘗試請求user_path時,它也會導致此錯誤。
錯誤:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User without an ID
我想這是因爲通過這個樣子的傳遞的PARAMS:
{"controller"=>"users", "action"=>"show", "format"=>"76"}
我是否正確使用奇異的資源呢?
我UsersController:
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
我的路線:
resources :users
match "profile" => "users#show"
什麼是你的'UsersController#show'方法是什麼樣子? routes.rb中是否有其他路線與用戶/配置文件有關? – Mischa 2011-05-09 08:30:14
提供包含我的路線和控制器的更新 – Coderama 2011-05-09 08:50:06
感謝您添加信息。我認爲你應該在下面回答我的問題。如果你有更多的問題,請告訴我。 – Mischa 2011-05-09 09:08:34