【发布时间】:2012-02-16 07:00:55
【问题描述】:
我有一个用户模型,可以创建关系以吸引关注者并关注其他人,我从 Rails 教程中学到的一切都很好。有一件事是,我正试图通过添加能够看到其他人的追随者并能够拥有关注/关注按钮选项的选项来将其带入下一步。显然,现有的代码将所有内容都放在了应有的位置(关注者的小资料、按钮等),但是当我单击按钮时,它只会跟随我自己或不跟随我自己,而不是我选择关注的特定人,如何我可以解决这个问题吗?非常感谢任何帮助!
这是以下和关注者页面的代码:
<div id='ContentContainer'>
<div class='ContentLeft'>
<% if @users.any? %>
<%= render @users %>
<%= will_paginate @users %>
<% end %>
</div>
</div>
这会导致呈现包含此内容的用户模板
<% unless current_user?(@user) %>
<div id="follow_form_small">
<% if current_user.followed_users?(@user) %>
<%= render 'unfollow_small' %>
<% else %>
<%= render 'follow_small' %>
<% end %>
</div>
<% end %>
然后分别关注和取消关注表单
<%= form_for current_user.relationships.build(followed_id: @user.id),
remote: true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", :class => 'FollowButton_small' %>
<% end %>
<%= form_for current_user.relationships.find_by_followed_id(@user),
html: { method: :delete },
remote: true do |f| %>
<%= f.submit "Unfollow", :class => 'UnFollowButton_small' %>
<% end %>
关系控制者
class RelationshipsController < ApplicationController
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
关系模型
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
用户模型这也被添加到它上面
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def followed_users?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
关注表单
<% unless current_user?(@user) %>
<div id="follow_form_small">
<% if current_user.followed_users?(@user) %>
<%= render 'unfollow_small' %>
<% else %>
<%= render 'follow_small' %>
<% end %>
</div>
<% end %>
当前用户
private
def current_user
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end
helper_method :current_user
新编辑
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
validate :not_following_himself
def not_following_himself
errors.add :followed_id,
if followed_id == follower_id
end
end
【问题讨论】:
-
你能告诉你关系控制器的创建方法吗?可能有一些错误。
-
一切看起来都是文件,将调试器放入 create 方法并查看 params 中的内容。
-
@Naveed 忘了添加这个,你能看看这个吗,因为这可能会影响它-编辑-实际上似乎我在那里添加了它,但问题是如果你看一下最后一次编辑,如果不删除 按钮根本不会显示,如果我删除它,它们会显示,但我收到了这个问题。
-
是当前用户吗?是你自己的帮手吗?不要想如果设计提供了这个。这很好,当然你不想关注自己。你删除这个检查并显示显示的关注按钮,然后如果你点击关注按钮,你将关注自己。@bdon 想法很好添加验证在关系模型中,用户不应该跟随自己。
-
你没有使用 Devise 吗?如果你不覆盖设计助手!我排除了“current_user”的来源?不是“当前用户”
标签: ruby-on-rails ruby ruby-on-rails-3 relationship