【问题标题】:NoMethodError in Users#show / undefined method `friendships' for #<Profile:0x007ff052f60b68> Rails#<Profile:0x007ff052f60b68> Rails 的用户#show / undefined method `friendships' 中的 NoMethodError
【发布时间】:2016-03-03 23:35:40
【问题描述】:

我真的不知道为什么会出现这个错误....今天早些时候这工作正常,然后我做了一些修改,显然我迷失了方向,现在当我启动我的应用程序时,我总是当我尝试以用户身份登录时出现此错误。

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

已编辑

在同一个视图中,我链接到同一条路线并且它有效,这是为什么呢?我的意思是这基本上是相同的链接? (见下文)

          <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>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 many-to-many


    【解决方案1】:
    def show
        @user = User.find(params[:id]).profile
    end
    

    这似乎是错误的,因为您将User.profile 分配给@user

    这可以解释错误undefined method 'friendships' for #&lt;Profile:0x007ff052f60b68&gt;,因为您的User 模型有has_many :friendships,但Profile 没有。

    注意:使用web_consolebetter_errors 可以真正帮助追踪此类问题,值得花一些时间进行设置。你会在浏览器中看到一个异常的 Ruby 控制台,只要输入 @user 就会告诉你这是一个 Profile 实例,而不是 User 实例。

    我做了一些修改,我显然忘记了

    另一个教训:尽可能少地改变,TEST,尽可能少地改变,TEST,并不断重复。这样,如果出现问题,您就可以确切地知道是哪些更改导致了错误。保持这个反馈回路尽可能短。这也是 better_errors 等工具真正有用的地方。

    如需更深入的解释,请参阅Feedback Loops in Software Development

    【讨论】:

    • 嗨@Carpetsmoker,你是对的......当我删除.profile时,错误消息更改为“#:0x007ff054110c28 的未定义局部变量或方法'用户' >" " :get do %>"
    • @DaðiHall 我想你想要@user 用于类变量,而不是user 用于不存在的局部变量。这就是错误所说的:undefined local variable or method.
    • 谢谢@Carpetsmoker,这很有帮助,我编辑了问题并添加了其他链接到相同的路径,效果很好,基本上是相同的链接
    • @DaðiHall IIRC user_profile_path(user) 不检查传递的对象是否是User 的实际实例,它只是抓取传递的任何对象的id 以创建像@987654341 这样的链接@(即鸭子输入所有内容)。如果每个User 总是有一个Profile,那么ID 可能是相同的;但这不能保证!所以你需要使用user_profile_path(user.profile)
    猜你喜欢
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多