【问题标题】:Rails: Follow/Following Relationship ButtonRails:关注/关注关系按钮
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 是的 current_user 是一个辅助方法,嗯,我怎样才能防止它跟随自己而只跟随别人?
  • 您可以在关系模型中添加验证,以确保用户无法关注自己。 validate :not_following_himself, def not_following_himself errors.add :followed_id,'You can't follow yourself' if follower_id == follower_id
  • 这有点令人困惑,我会根据您上面的声明发布尽可能多的内容,已编辑
  • errors.add :followed_id,"Your can't follow yourself" if follower_id == follower_id#至少这个验证不会允许用户关注自己。现在您可以通过放置调试器或简单地使用“puts params.inspect”来添加查看参数中的内容,然后查看您的控制台。尝试调试问题,测试 current_user 的 id 输出。我不确定您在哪里添加 cookie,如果没有 cookie,您正在将 nil 与 User 进行比较,那将始终返回 false 并且您的故事相同。为所有用户显示的关注按钮
【解决方案2】:

我认为这里没有足够的代码来确定问题所在。您还应该提供控制器操作。

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

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

【讨论】:

    【解决方案3】:

    我发现我的代码与您的代码 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
    

    在 user.rb 中

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

    希望我的代码有帮助!!

    【讨论】:

      【解决方案4】:

      我正在为追随者使用验证,现在,我为追随者创建方法

      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
      

      我的代码是真的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-21
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 2017-12-24
        相关资源
        最近更新 更多