2013-02-25 75 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