繁体   English   中英

为什么会出现路由错误?

[英]Why do I get a routing error?

我有这个链接:

<%= link_to "Profile", user_profile_path(current_user) %>

当我尝试访问个人档案控制器中的show时,它给了我一个路由错误。

这是我的routes.rb:

resources :users do
  resources :profiles
end

这是我的个人资料控制器中的show方法:

def show
  @profile = @user.profiles.find(params[:id])
end

我的用户模型中也有此回调:

before_create :build_profile

我究竟做错了什么?

您缺少个人资料ID。

像这样:

<%= link_to "Profile", user_profile_path(:user_id => current_user.id, :id => profile.id) %>

编辑

这很肮脏,您可能一开始就不应该嵌套这些对象,但这可能会让您摆脱当前的困扰。

<%= link_to("Profile", user_profile_path(:user_id => current_user.id, :id => current_user.profile.id)) unless current_user.profile.blank? %>

您应该认真考虑在路由中取消嵌套这些内容,并仅基于其自身的ID(而不是用户的ID)提供对配置文件的访问权限。

resources :users
resources :profiles

<%= link_to("Profile", profile_path(current_user.profile)) unless current_user.profile.blank? %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM