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
4

3 に答える 3

2

このような検証は、モデル レベルで行うのが最善だと思います。モデルでvalidate_uniqueness_ofを使用し、コントローラーで有効性をテストします。

于 2012-07-09T16:09:41.397 に答える
0

私は友人だと思いますか?メソッドは次のようになります。

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

友情は user_id と friend_id を持つテーブルであるべきだと思うので、チェックしたいのは、このユーザーが友人として other_user を持っているかどうかです。

于 2012-07-09T16:16:45.283 に答える
0

あなたのメソッドはorfriend?を返していますが、メソッドで期待するようにorではありません。friendshipniltruefalse?

試す

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
于 2012-07-09T16:04:07.850 に答える