I'm having issues creating Friendships in Rails 3 beta.

I'm following Railscast 163 (
http://asciicasts.com/episodes/163-self-referential-association) and this is
what I have in the relevant files:

*## CONTROLLERS*

# application_controller.rb
class ApplicationController < ActionController::Base
  def current_user
    User.find(session[:user_id])
  end
end

# friendships_controller.rb
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[:notice] = 'Unable to add friend.'
      redirect_to users_path
    end
  end
end

*## MODELS*

# friendship.rb
class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => 'User'
end

# user.rb
class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, :through => :friendships
  # rest snipped for brevity
end

*## VIEWS*

# users/index.html.haml
= link_to 'Add Friend', friendships_path(:friend_id => user), :method =>
:post

# users/show.html.haml
#profile
  .section
    %h3= @following_title
    %ul
      - if @user.friendships != []
        - for friendship in @user.friendships
          %li= friendship.friend.name
      - else
        %li
          Nobody yet.

*## OTHER*

# routes.rb
SampleApp::Application.routes.draw do |map|
  resources :friendships
  resources :sessions
  resources :users do
    resources :posts
  end
end

# xxxx_create_friendships.rb
class CreateFriendships < ActiveRecord::Migration
  def self.up
    create_table :friendships do |t|
      t.integer :user_id
      t.integer :friend_id

      t.timestamps
    end
  end

  def self.down
    drop_table :friendships
  end
end


*The problem:*
When I'm on http://localhost:3000/users/ and I try to 'Add Friend' user_3
(which should POST with http://localhost:3000/friendships?friend_id=3), I
get the following page:

Unknown action
The action 'index' could not be found

It's clearly because there isn't a 'friendships/index' page. However, I
don't understand why the application is even trying to reach
'friendships/index'. The code should be redirecting the current user to
their profile page after the friendship is created.

When I manually go to the current user's profile page, I see that no
friendships are listed--which means that my code for 'Add Friend' isn't
saving the friendship to the database either.

I'm probably missing something very simple, but I'm very new to Rails.
Thanks in advance for any help.

-- 
Peng Zhong

Web & UX Desiger
http://nylira.com

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to