0
我有自我指涉關聯工作。我的問題是,在users/show上,我想根據用戶與當前用戶的關係顯示不同的文本。Follow Model的條件鏈接
目前,如果用戶=當前用戶,我將它設置爲不顯示任何內容。如果用戶不是當前用戶,並且不是當前用戶的朋友,我想顯示一個鏈接以跟隨用戶。最後,如果用戶不是當前用戶,並且已經是當前用戶的朋友,我想顯示文本以說「朋友」。
friendship.rb
belongs_to :user
belongs_to :friend, :class_name => "User"
user.rb
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
用戶/顯示
<% unless @user == current_user %>
<%= link_to "Follow", friendships_path(:friend_id => @user), :method => :post %>
<% end %>