Ruby on rails 3 Rails 3-嵌套资源路由-一对一关系
某些嵌套资源路由有问题。我试图做的是链接到用户的个人资料页面进行编辑。在我看来,它是这样写的:Ruby on rails 3 Rails 3-嵌套资源路由-一对一关系,ruby-on-rails-3,routing,nested-resources,Ruby On Rails 3,Routing,Nested Resources,某些嵌套资源路由有问题。我试图做的是链接到用户的个人资料页面进行编辑。在我看来,它是这样写的: <%= link_to "Edit Profile", edit_user_profile_path(current_user) %> 我检查了我的Rake路线,它给了我一个有效的选项: edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) {:action=>"edit", :controll
<%= link_to "Edit Profile", edit_user_profile_path(current_user) %>
我检查了我的Rake路线,它给了我一个有效的选项:
edit_user_profile GET /users/:user_id/profiles/:id/edit(.:format) {:action=>"edit", :controller=>"profiles"}
我可以手动导航到它。对于良好的措施,以下是我的控制器的证明:
class ProfilesController < ApplicationController
def edit
@user = current_user
@profile = current_user.profile
end
def update
@user = current_user
@profile = current_user.profile
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.html { redirect_to(orders_path, :notice => "Your profile has been updated.") }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @profile.errors, :status => :unprocessable_entity }
end
end
end
end
class ProfilesController“您的配置文件已更新。”)}
format.xml{head:ok}
其他的
format.html{render:action=>“edit”}
format.xml{render:xml=>@profile.errors,:status=>:unprocessable_entity}
结束
结束
结束
结束
不管怎样,我在追踪这件事上遇到了一些问题。任何指示都会有帮助。对于my DB,设计概要文件属于一对一关系中的用户。我希望这只是一些我没有注意到的新手,一双新眼睛可能会有所帮助。如果你仔细观察你的路线,你会发现它需要一个
:user\u id
和一个:id
。在本例中,后者指的是用户配置文件
为了告诉Rails您想要那个特定的概要文件,您必须在链接中指定用户和概要文件,如下所示:
edit_user_profile_path(current_user, @profile)
现在,Rails将使用第一个参数(
current\u user
)作为路由的:user\u id
部分,第二个参数(@profile
)作为:id
,多谢Voncorad!我真的很感谢你的帮助。
class ProfilesController < ApplicationController
def edit
@user = current_user
@profile = current_user.profile
end
def update
@user = current_user
@profile = current_user.profile
respond_to do |format|
if @profile.update_attributes(params[:profile])
format.html { redirect_to(orders_path, :notice => "Your profile has been updated.") }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @profile.errors, :status => :unprocessable_entity }
end
end
end
end
edit_user_profile_path(current_user, @profile)