【问题标题】:undefined method `follow' from User.rb来自 User.rb 的未定义方法“follow”
【发布时间】:2013-11-13 19:38:12
【问题描述】:

Rails 3.2 新版本。

该应用是推特。

更新 3:已取得进展。我在buddies.html.erb 中将@phriends 更改为:phriends,现在通知user don't exist 自动出现.. 但至少页面加载.. 然后“加/减”给我"No route matches [POST] "/buddies"

更新 2:"undefined methodmodel_name' for NilClass:Class"` for @phriends...我不认为 rails 喜欢我在一个页面上做了很多事情。

更新:"Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" 我在两次不同的时间两次收到此错误。

这来自我的 User.rb 模型

  def following? user
self.followeds.include? user
 end

 def follow user
Relationship.create follower_id: self.id, followed_id: user.id
 end

 def unfollow user
   Relationship.destroy follower_id: self.id, followed_id: user.id
 end

到我的 user_controller.rb

def buddies
@phriends = User.find_by_username(params[:username])

                if current_user.following? @phriends
                        current_user.unfollow @phriends 
                        else
                    current_user.follow @phriends 
                    end

到views/user/buddies.html.erb

<%= form_for @phriends do |f| %>
<%= f.text_field :username, placheholder:"username" %>
<%= f.submit "Add/Subtract" %>

如您所知,我正在尝试通过输入正确的用户名来关注/取消关注。

我觉得我错了,因为那行不通。

控制器的另一个选项是这个......

 @phriends = User.find_by_username(params[:username])  
                if current_user.following? @phriends
                        relationship_path, method: :delete
                        else
                    @relationship
                    end

                 @relationship = Relationship.where(
                                  follower_id: current_user.id,
                                   followed_id: @user.id
                                  ).first_or_initialize if current_user

然后它抱怨我没有 ID.. 无法破解最后一个问题。

顺便说一句,这是一个一体化的页面..有点违反规则。

附:额外积分:此页面还显示了我关注的所有推文...我怎么说只能显示每个用户最近的推文...?

【问题讨论】:

  • 不知道方法followuser_controller.rb的问题吗?
  • 我不知道是不是应该这样,但是是的。我从教程中复制了它。
  • 更新并修复了原始错误。现在我得到“为 nil 调用 id,它会错误地为 4——如果你真的想要 nil 的 id,请使用 object_id”我在两种不同的设置中得到了两次。

标签: ruby-on-rails ruby ruby-on-rails-3 twitter


【解决方案1】:

如果问题仅出现在控制器中未定义的方法中,那么这很容易 - 您应该从 current_user 调用方法

def buddies
  @phriends = User.find_by_username(params[:username])
  if @phriends
    if current_user.following? @phriends
      current_user.unfollow @phriends
    else
      current_user.follow @phriends
    end
  else
    # @phriends is nil do smth for example
    flash[:notice] = "User with username #{params[:username]} is not found"
  end
end

更新:要小心,因为代码 @phriends = User.find_by_username(params[:username]) 可以返回 nil,而您可以得到 nil.some_method。您可以添加检查 @phriends 是否不为零

红宝石中的 PS nil 与布尔值的 false 相同

【讨论】:

  • 刚刚同时修复,你的权利。现在我明白了。 "为 nil 调用 id,会误为 4 -- 如果你真的想要 nil 的 id,请使用 object_id"
  • 谢谢,有人可以进一步解释一下吗?
  • 据我了解,您在页面上有一些输入或选择,用户输入/选择 username。您找到使用find_by_username 的用户。如果找不到这样的用户,则 nil 将返回到变量@phriends。如果您的用户为零,则无法关注/取消关注。我认为您应该定义一些消息(例如 flash)并将其显示到页面(通知 current_user 未执行操作跟随/取消关注)
  • 是的,我早些时候试图弄清楚这一点。我需要一种方法来验证没有空白和存在是真的......所以在@phriendsif-statement 里面我应该说:if @phriends = nil , notice: "User don't exist" 这是否涵盖了所有可能的错误?
  • controller中的方法可以如上(我更新了)。
猜你喜欢
  • 2017-04-19
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 2012-05-17
  • 2020-11-25
  • 2021-07-30
相关资源
最近更新 更多