2012-07-09 90 views
0

我需要幫助關於友誼的獨特性,並加入到友情鏈接 我的routes.rb:獨特的友誼需要幫助的Rails 3

devise_for :users 

resources :users, only: [:index, :show] do 
      resources :friendships, only: [:create, :destroy] 
      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" 
end 

friendships_controller.rb:

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.rb:

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 
end 

這是我的朋友添加鏈接:

<h1> 
     <%= @user.username %> 
     </h1> 

      <%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id => @user), :method => :post,class: "btn btn-large btn-primary" %> 

,我不能找到顯示用戶的朋友的正確路徑。

<% for friendship in @friendships %> 
<%= link_to friendship.friend.username, '#' %> 
(<%= link_to 'Sil', friendship, :method => :delete %>) 
<% end %> 

任何人都可以幫我嗎?

+0

在控制檯中您可以運行'rake routes',這將顯示所有可用的路線及其命名 – tommasop 2012-07-09 11:51:40

回答

1

鑑於嵌套路線,它將需要知道用戶以獲得友誼。您可以使用酷傭工是讓你從對象的路徑

多一點地道的紅寶石般

<% @friendships.each do |friendship} %> 
    <%= link_to friendship.friend.username, '#' %> 
    (<%= link_to 'Sil', [@user, friendship], :method => :delete %>) 
<% end %> 

這將是對「加爲好友」鏈接相似...但我在邏輯上不清楚 - 你在這個階段擁有哪些用戶? @user是登錄用戶還是「當前」用戶?

由於您使用的設計......這可能是因爲你在上面真正想要的是:

<% current_user.friendships.each do |friendship} %> 
    <%= link_to friendship.friend.username, '#' %> 
    (<%= link_to 'Sil', [current_user, friendship], :method => :delete %>) 
<% end %> 

如果該片段是從另一個用戶的網頁...然後它的東西更像

<h1><%= @user.username %></h1> 

<%= link_to "Arkadaşlarıma Ekle", user_friendships_path(current_user, :friend_id => @user), :method => :post, class: "btn btn-large btn-primary" %> 
+0

這完全取決於您要使用多少javascript以及您所使用的用戶體驗。我會做沒有嵌套的屬性 - 只需在友誼控制器中調用現有的'create'動作即可。 – mylescarrick 2012-07-09 22:54:01