0

i am following this post for adding users as friends in ruby on rails

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

I am able to add users as friends via rails console but not via the actual page...this is my controller code

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

Model: User:

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

friends:

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

view:

<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%>

my view is working correctly .. if i add friends via console and commit it, it displays in the view.. where did i go wrong here??

5
  • how are you getting params[:friend_id]? you should post your model and your view.
    – jvnill
    Feb 25, 2013 at 11:39
  • i am just displaying the firend's email id in view file Feb 25, 2013 at 11:44
  • you misunderstood what view I'm asking for. can you show the view where the user most probably selects the friend they want to add?
    – jvnill
    Feb 25, 2013 at 11:48
  • On this line: <br/><%= variable.email %> &nbsp; <%= link_to 'Add as Friend' , friendships_path(:friend_id => @user), :method => :post %>, what happens if you change @user to @user.id? If it doesn't work, can you post a log trace. Feb 25, 2013 at 12:05
  • cannot give user.id as mass accessing is prohibited in rails 3 Feb 25, 2013 at 14:54

1 Answer 1

0

I was able to correct it..had wrong code in my view file.. thanks a lot jvnill....had it not been for you, I would not have checked my view file... :)

changing friendships_path(:friend_id => @user) to friendships_path(:friend_id => variable) did the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.