2012-07-09 74 views
0

我與朋友模型卡住了。我可以多次與用戶成爲朋友。所以我需要一個條件來避免添加到朋友鏈接。 我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 

回答

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 
+0

我得到的synthax錯誤意外的':'期待關鍵字結束,它顯示錯誤之前的空間錯誤 – ytsejam 2012-07-09 16:11:00

+0

其實它並不重要,因爲在ruby中'nil'在布爾上下文中評估爲'false'並且友誼將會評估爲'真的' – davidrac 2012-07-09 16:13:41

+0

其實該網站無法以這種方式工作?我已經找不到方法了。 – ytsejam 2012-07-09 17:03:51

2

我認爲這是最好的模型級別做這樣的驗證。在您的模型中使用validate_uniqueness_of並在您的控制器中測試有效性。

+0

我是否可以在用戶或友誼模型中使用它。 – ytsejam 2012-07-09 16:23:06

+0

可能就是友誼模式。您應該在另一個用戶的範圍內驗證給定用戶的唯一性。 – davidrac 2012-07-09 16:51:25

+0

validates_uniqueness_of:user_id,:scope => [:friend_id]? – ytsejam 2012-07-09 16:57:53

0

我認爲朋友?方法應該是類似的東西:

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

,因爲我想的友誼應該與USER_ID和friend_id一個表,我們要檢查的是如果該用戶已經othter_user爲朋友或沒有。

+0

您對視圖有什麼建議。我得到'朋友'的未定義方法? – ytsejam 2012-07-09 16:20:26