【问题标题】:How to avoid being friends multiple times如何避免多次成为朋友
【发布时间】:2012-07-09 01:43:42
【问题描述】:

我坚持使用友谊模型。我可以和一个用户多次成为朋友。所以我需要一个条件来避免添加到朋友链接。 我的 users_controller :

class UsersController < ApplicationController
    before_filter :authenticate_user!

  def index
        @users = User.all
  end
  def show
    @user = User.find(params[:id])
    @topics = @user.topics.paginate(page: params[:page])
    @friendship = @user.friendships.build(:friend_id => params[:friend_id])
    @friendships = @user.friendships.all

  end

我的 show.html.erb:

       <section>
            <h1><%= @user.username %></h1>     
       <%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id => @user), :method => :post,class: "btn btn-large btn-primary" %>

            </section>  

我的friendships_controller:

class FriendshipsController < ApplicationController
    before_filter :authenticate_user!

    def create
        @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
        if @friendship.save
            flash[:notice] = "Arkadaşlara eklendi."
            redirect_to root_url
        else
            flash[:error] = "Arkadaşlara Eklenemiyor."
            redirect_to root_url
        end
    end

    def destroy
        @friendship = current_user.friendships.find(params[:id])
        @friendship.destroy
        flash[:notice] = "Arkadaşlarımdan kaldırıldı."
        redirect_to current_user
    end
end  

所以我想,我尝试在 users_controller 中添加此方法,但仍然没有解决方案。你能帮我解决它吗?

def friend?(other_user)
   friendships.find_by_friend_id(other_user.id)
 end

和在链接之前

<% unless friend?(@user) %>
%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id => @user), :method => :post,class: "btn btn-large btn-primary" %>
<%end %> 

我的用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username
  # attr_accessible :title, :body
  has_many :topics
  has_many :posts
  has_many :friendships
    has_many :friends, :through => :friendships
    has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
    has_many :inverse_friends, :through => :inverse_friendships, :source => :user
end

我的友谊模型:

    class Friendship < ActiveRecord::Base
      attr_accessible :friend_id, :user_id
      belongs_to :user
        belongs_to :friend, :class_name => "User"
    validates :friend, :presence => true, :unless => :friend_is_self

def friend_is_self
  user_id == friend_id ? false : true
end
end

【问题讨论】:

    标签: ruby-on-rails conditional-statements rails-activerecord


    【解决方案1】:

    您的friend? 方法返回的是friendshipnil,而不是truefalse,正如您对? 方法所期望的那样。

    试试

    def friend?(other_user)
      # this assumes Friendship is a user, which probably isn't true
      # but I don't have your models but this should give you the right idea
      self.friendships.include other_user
    end
    

    【讨论】:

    • 我得到 synthax 错误意外':'期待关键字结束,它在错误之前的空格中显示错误
    • 实际上这并不重要,因为在 ruby​​ 中,nil 在布尔上下文中被评估为 false,而友谊将被评估为 true
    • 实际上该网站无法以这种方式运行?我已经找不到方法了。
    【解决方案2】:

    我认为最好在模型级别进行此类验证。在您的模型中使用 validate_uniqueness_of 并在您的控制器中测试其有效性。

    【讨论】:

    • 我应该在用户或友谊模型中使用它吗?
    • 可能在友谊模型上。您应该在另一个用户的范围内验证给定用户的唯一性。
    • 是的。我认为应该这样做。
    • 所以在视图中..如果他们是朋友,我如何测试他们?你能检查我最后的编辑。我做了 current_user 测试,但没有做朋友测试。
    • if @friendship.save 行也应该验证。您将在错误中得到错误并显示给用户。
    【解决方案3】:

    我觉得朋友?方法应该类似于:

    def friend?(other_user)
       current_user.friendships.find_by_friend_id(other_user.id)
    end
    

    因为我猜friendship应该是一个有user_id和friend_id的表,所以我们要检查的是这个用户是否有other_user作为朋友。

    【讨论】:

    • 您对视图有什么建议。我得到“朋友”的未定义方法?
    猜你喜欢
    • 2015-11-23
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2012-10-26
    • 1970-01-01
    • 2011-05-20
    • 2011-08-10
    相关资源
    最近更新 更多