如何多次避免成为朋友

时间:2012-07-09 15:57:51

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

我坚持使用友谊模式。我可以多次与用户成为朋友。所以我需要一个条件来避免添加到朋友链接。 我的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>  

my 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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您的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

答案 2 :(得分:0)

我认为这位朋友?方法应该类似于:

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

因为我猜友谊应该是一个包含user_id和friend_id的表,我们要检查的是该用户是否将othter_user作为朋友。

相关问题