2016-03-28 19 views
0

Kullanıcılar ile aynı türden bir sistem oluşturuyorum (Devise). Ryan Bates Rails'i takip ettim: http://railscasts.com/episodes/163-self-referential-associationRaylar: Kullanıcı arabirimine gitmek için doğru yol Arkadaş olarak ekledim

Bu kodda arkadaş ekleyebilir ve arkadaşları silebiliriz. Hesaplarını yönetmek için user#show görünümü oluşturuyorum.

Resimde bir link_to oluşturmak ve profiline gitmek için eklediğim arkadaş adını oluşturmak istiyorum. Bir arkadaşı eklediğinizde

Burada

<ul class="list-unstyled" > 
    <% for friendship in @user.friendships %> 
    <li class="col-sm-6 col-sm-3 col-md-3 col-md-offset"> 
     <div class="friend-picture"> 
     <%=h image_tag friendship.friend.picture %> 
      </div> 
      <h6> 
      <%=h friendship.friend.first_name %> <%=h friendship.friend.last_name[0] %>. 
       <%= link_to friendship, :method => :delete do %> 
       <i class="fa fa-times fa-lg"></i> 
      <% end %> 
      </h6> 
     </li> 
     <% end %> 
    </ul> 

göç ​​dostluğu

class CreateFriendships < ActiveRecord::Migration 
    def change 
    create_table :friendships do |t| 
     t.integer :user_id 
     t.integer :friend_id 

     t.timestamps null: false 
    end 
    end 
end 
denir şovum görünümünde gibi görüntülemek istediğiniz

User modeli

has_many :friendships 
has_many :friends, :through => :friendships 
olduğunu

Friendship modelinin modeli

'dir.
belongs_to :user 
belongs_to :friend, :class_name => "User" 

class FriendshipsController < ApplicationController 
    def create 
    @friendship = current_user.friendships.build(:friend_id => params[:friend_id]) 
    if @friendship.save 
     flash[:notice] = "Added friend." 
     redirect_to current_user 
    else 
     flash[:error] = "Unable to add friend." 
     redirect_to current_user 
    end 
    end 

def destroy 
    @friendship = current_user.friendships.find(params[:id]) 
    @friendship.destroy 
    flash[:notice] = "Removed friendship." 
    redirect_to current_user 
    end 
end 

cevap

1

izlemeyi deneyin Cevabınız için teşekkür ederiz Friendship kontrolör:

<ul class="list-unstyled" > 
<% @user.friendships.each do |friendship| %> 
    <li class="col-sm-6 col-sm-3 col-md-3 col-md-offset"> 
    <div class="friend-picture"> 
     <%= link_to user_path(friendship.friend) do %> 
     <%= h image_tag friendship.friend.picture %> 
     <% end %> 
    </div> 
    <h6> 
     <%= link_to user_path(friendship.friend) do %> 
     <%=h friendship.friend.first_name %> <%=h friendship.friend.last_name[0] %> 
     <% end %> 
     <%= link_to friendship, :method => :delete do %> 
     <i class="fa fa-times fa-lg"></i> 
     <% end %> 
    </h6> 
    </li> 
<% end %> 
</ul> 

Ben de daha ruby olduğunu each için for döngü, güncellemek yol.

+0

Teşekkür ederiz @Dharam Neden 'for' ile çalışmıyor? Anlamıyorum –

+1

Ben '' için döngü demedim demedim. Sadece 'her' iterasyon için kullanmak 'ruby' yoludur. – Dharam

+0

Tamam, çünkü 'link_to' için sorun oldu –

İlgili konular