在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