NoMethodError in Users#show / 未定义的方法`friendships' for #<Profile:0x007ff052f60b68> Rails
NoMethodError in Users#show / undefined method `friendships' for #<Profile:0x007ff052f60b68> Rails
我真的不明白为什么我会收到这个错误....今天早些时候它工作正常,然后我做了一些修改,但我显然迷路了,现在当我启动我的应用程序时当我尝试以用户身份登录时总是出现此错误。
NoMethodError in Users#show
undefined method `friendships' for #<Profile:0x007ff052f60b68>
我的应用程序中有 3 个用户,他们都有个人资料。
在 current_user 个人资料页面上,用户可以看到他的朋友并点击他们的名字来查看他们的个人资料。
谁能帮我解决这个问题?
TIA大地
在views/users/show.html.erb
<h4> <%= current_user.profile.name%> Friends</h4>
<ul>
<% @user.friendships.each do |friendship| %>
<li>
<%= link_to user_profile_path(user), :method => :get do %>
<%= friendship.friend.profile.name %>
<%#= link_to compare_friends_path(@user), :method => :get do %>
<%#= friendship.friend.profile.name %>
(<%= link_to "remove friend", friendship, :method => :delete %>)
</li>
<% end %>
<% end %>
</ul>
在users_controller.rb
def show
@user = User.find(params[:id]).profile
end
在user.rb模型中
has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, through: :inverse_friendships, :source => :user
has_one :profile
在profiles_controller.rb
def show
@user = User.find(params[:user_id])
#@profile = @user.profile
end
在profile.rb模型中
class Profile < ActiveRecord::Base
belongs_to :user
end
在friendship.rb模型中
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User'
end
在routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users do
resource :profile
end
resources :friendships
end
已编辑
在同一视图中,我link正在前往同一路线并且它有效,这是为什么呢?我的意思是这个基本一样link? (见下文)
<h3>followers <%= current_user.profile.name%> </h3>
<ul>
<% @user.inverse_friends.each do |user| %>
<%= link_to user_profile_path(user), :method => :get do %>
<li><%= user.profile.name %></li>
<% end%>
<% end %>
</ul>
def show
@user = User.find(params[:id]).profile
end
这似乎是错误的,因为您正在将 User.profile
分配给 @user
。
这可以解释错误 undefined method 'friendships' for #<Profile:0x007ff052f60b68>
,因为您的 User
模型有 has_many :friendships
,但 Profile
没有。
注意:使用 web_console or better_errors 确实可以帮助追踪此类问题,值得花一些时间进行设置。你在浏览器中看到一个异常的 Ruby 控制台,只要输入 @user
就会告诉你这是一个 Profile
实例,而不是 User
实例。
I did some modifications that I clearly lost track off
另一个教训:尽可能少地改变,TEST,尽可能少地改变,TEST,并不断重复这个。这样,如果出现问题,您就可以确切地知道是哪些更改导致了错误。使此反馈回路尽可能短。这也是 better_errors
等工具真正有用的地方。
如需更深入的解释,请参阅 Feedback Loops in Software Development。
我真的不明白为什么我会收到这个错误....今天早些时候它工作正常,然后我做了一些修改,但我显然迷路了,现在当我启动我的应用程序时当我尝试以用户身份登录时总是出现此错误。
NoMethodError in Users#show
undefined method `friendships' for #<Profile:0x007ff052f60b68>
我的应用程序中有 3 个用户,他们都有个人资料。
在 current_user 个人资料页面上,用户可以看到他的朋友并点击他们的名字来查看他们的个人资料。
谁能帮我解决这个问题? TIA大地
在views/users/show.html.erb
<h4> <%= current_user.profile.name%> Friends</h4>
<ul>
<% @user.friendships.each do |friendship| %>
<li>
<%= link_to user_profile_path(user), :method => :get do %>
<%= friendship.friend.profile.name %>
<%#= link_to compare_friends_path(@user), :method => :get do %>
<%#= friendship.friend.profile.name %>
(<%= link_to "remove friend", friendship, :method => :delete %>)
</li>
<% end %>
<% end %>
</ul>
在users_controller.rb
def show
@user = User.find(params[:id]).profile
end
在user.rb模型中
has_many :friendships
has_many :friends, through: :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, through: :inverse_friendships, :source => :user
has_one :profile
在profiles_controller.rb
def show
@user = User.find(params[:user_id])
#@profile = @user.profile
end
在profile.rb模型中
class Profile < ActiveRecord::Base
belongs_to :user
end
在friendship.rb模型中
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User'
end
在routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users do
resource :profile
end
resources :friendships
end
已编辑
在同一视图中,我link正在前往同一路线并且它有效,这是为什么呢?我的意思是这个基本一样link? (见下文)
<h3>followers <%= current_user.profile.name%> </h3>
<ul>
<% @user.inverse_friends.each do |user| %>
<%= link_to user_profile_path(user), :method => :get do %>
<li><%= user.profile.name %></li>
<% end%>
<% end %>
</ul>
def show
@user = User.find(params[:id]).profile
end
这似乎是错误的,因为您正在将 User.profile
分配给 @user
。
这可以解释错误 undefined method 'friendships' for #<Profile:0x007ff052f60b68>
,因为您的 User
模型有 has_many :friendships
,但 Profile
没有。
注意:使用 web_console or better_errors 确实可以帮助追踪此类问题,值得花一些时间进行设置。你在浏览器中看到一个异常的 Ruby 控制台,只要输入 @user
就会告诉你这是一个 Profile
实例,而不是 User
实例。
I did some modifications that I clearly lost track off
另一个教训:尽可能少地改变,TEST,尽可能少地改变,TEST,并不断重复这个。这样,如果出现问题,您就可以确切地知道是哪些更改导致了错误。使此反馈回路尽可能短。这也是 better_errors
等工具真正有用的地方。
如需更深入的解释,请参阅 Feedback Loops in Software Development。