【发布时间】: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.. 无法破解最后一个问题。
顺便说一句,这是一个一体化的页面..有点违反规则。
附:额外积分:此页面还显示了我关注的所有推文...我怎么说只能显示每个用户最近的推文...?
【问题讨论】:
-
不知道方法
follow是user_controller.rb的问题吗? -
我不知道是不是应该这样,但是是的。我从教程中复制了它。
-
更新并修复了原始错误。现在我得到“为 nil 调用 id,它会错误地为 4——如果你真的想要 nil 的 id,请使用 object_id”我在两种不同的设置中得到了两次。
标签: ruby-on-rails ruby ruby-on-rails-3 twitter