Rails:跟随/关注关系按钮

时间:2012-02-16 07:00:56

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

我有一个用户模型,可以创建关系以拥有关注者并关注其他人,我从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

4 个答案:

答案 0 :(得分:1)

问题与current_user?方法有关。它可能总是返回true并且在用户列表中跟随按钮会显示给所有用户(包括您的用户),因此通过点击跟随您可以关注自己。

答案 1 :(得分:0)

我认为这里没有足够的代码来确定问题究竟是什么。您也应该提供控制器操作。

您可以采取以下步骤进行调试:

  1. 关系/创建的控制器操作应该使用params [:followed_id]和current_user.id。
  2. 在关系中添加验证,其中包含follow_id!= follower_id以及两个ID都存在。
  3. 查看“关注”表单上生成的HTML,并确保follow_id的隐藏字段的值是正确的。该ID应与当前登录的用户不同。

答案 2 :(得分:0)

我发现我的代码与您的代码相同95%。但是我不使用form_for,而是使用link_to:

<强>的routes.rb

resources :relationships, :only => [:create, :destroy]

在我看来:

<%= link_to "Follow", relationships_path(:id => @user.id), :method => :post, :remote => 'true', :class => "FollowButton_small"%>

<强> relationships_controller.rb

def create
  @followed = User.find(params[:id])
  current_user.follow!(@followed)
  respond_to do |format|
    format.js { @user = @followed}
  end
end

in user.rb

def follow!(followed)
  relationships.create!(:followed_id => followed.id)
end

希望我的代码有所帮助!!

答案 3 :(得分:0)

我正在使用验证关注者,现在,我为关注者创建方法

class Relationship < ApplicationRecord
  belongs_to :follower, class_name: User.name 
  belongs_to :followed, class_name: User.name 

  validate :follower, on: :create
  validate :followed, on: :create

  def follower
    errors.add :follower,
    unless current_user.follow @user
  end

  def followed
    errors.add :followed,
    unless current_user.unfollow @user
  end
end

我的代码是真的吗?