为什么会出现路由错误?

时间:2022-08-22 22:52:06

I've got this link:

我有这个链接:

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

and it gives me a routing error when I'm trying to access show in the profiles controller.

当我试图访问profiles控制器中的show时,它会给我一个路由错误。

Here is my routes.rb:

这是我routes.rb:

resources :users do
  resources :profiles
end

And here's my show method in the profiles controller:

这是我在profiles控制器中的show方法:

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

I also have this callback in my User model:

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

before_create :build_profile

What am I doing wrong?

我做错了什么?

1 个解决方案

#1


1  

You're missing the profile ID.

您丢失了配置文件ID。

Something like this:

是这样的:

<%= 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 => profile.id) %>

edit

编辑

This is pretty filthy, and you should probably not be nesting these objects in the first place, but this will probably get you past your current issue.

这是相当脏的,而且您可能不应该首先嵌套这些对象,但是这可能会让您克服当前的问题。

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

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

You should seriously consider un-nesting these in your routes and simply provide access to a profile based on its own ID, and not the ID of the user.

您应该认真地考虑在您的路由中嵌套un- these,并简单地根据配置文件本身的ID(而不是用户的ID)提供对其的访问。

resources :users
resources :profiles

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

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

#1


1  

You're missing the profile ID.

您丢失了配置文件ID。

Something like this:

是这样的:

<%= 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 => profile.id) %>

edit

编辑

This is pretty filthy, and you should probably not be nesting these objects in the first place, but this will probably get you past your current issue.

这是相当脏的,而且您可能不应该首先嵌套这些对象,但是这可能会让您克服当前的问题。

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

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

You should seriously consider un-nesting these in your routes and simply provide access to a profile based on its own ID, and not the ID of the user.

您应该认真地考虑在您的路由中嵌套un- these,并简单地根据配置文件本身的ID(而不是用户的ID)提供对其的访问。

resources :users
resources :profiles

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

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