2013-02-25 80 views
0

紅寶石添加好友我下面這個職位上軌在軌道上

http://asciicasts.com/episodes/163-self-referential-association

我能夠通過軌道控制檯,但不是通過實際的頁面添加用戶的朋友在Ruby中添加用戶作爲朋友。 ..this是我的控制器代碼

class FriendshipsController < ApplicationController 
    def new 
    end 

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

    def show 
    end 
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 
has_many :contacts 
has_many :phones 
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 
    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :user_id 
    # attr_accessible :title, :body 
end 

朋友:

class Friendship < ActiveRecord::Base 
    attr_accessible :friend_id, :user_id 
    belongs_to :user 
    belongs_to :friend, :class_name => 'User' 
end 

觀點:

<h1>Phones#index</h1> 
<center>Users in the system<center><br/> 
<p><% @phones.each do |variable|%> 
    <% if @phn.email!= variable.email %> 
    <br/><%= variable.email %> &nbsp; <%= link_to 'Add as Friend' , friendships_path(:friend_id => @user), :method => :post %> 
    <%end%> 
    <%end%> 
<p>friends</p> 
<% for user in current_user.friends %> 
<p><%= user.email %></p> 
<%end%> 

我的看法是正確的工作..如果我通過控制檯添加好友並提交它,它會在視圖.. 在哪裏我做錯這裏??

+0

你好嗎'PARAMS [:friend_id]'?你應該發佈你的模型和你的觀點。 – jvnill 2013-02-25 11:39:26

+0

我只是在查看文件 – 2013-02-25 11:44:52

+0

中顯示了firend的電子郵件ID,您誤解了我所要求的視圖。你能否顯示用戶最可能選擇他們想要添加的朋友的視圖? – jvnill 2013-02-25 11:48:11

回答

0

我能糾正它..在我的視圖文件錯誤的代碼..謝謝很多jvnill ....有沒有爲你,我不會檢查我的視圖文件... :)

改變friendships_path(:friend_id => @user)friendships_path(:friend_id => variable) did the trick