Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller path for nested resource - undefined method `<controller>_path'

I'm having trouble displaying my form at /users/2/friends/new. I'm receiving

undefined method `friends_path' for #<#<Class:0x21f0c14>:0x21ef364>

Here is the beginning of the form

<% form_for(@friend) do |f| %> 

And the friends controller

def new
     @user = User.find(params[:user_id])
     @friend = @user.friends.build
end

This is the route

resources :users do
       resources :friends
end

And the relevant path from "rake routes"

users/:user_id/friends/new(.:format)      {:controller=>"friends", :action=>"new"}

Any help or insight is greatly appreciated. This is my first rails 3 app.

like image 984
owilde1900 Avatar asked Dec 24 '10 19:12

owilde1900


1 Answers

Try:

user_friends_path(@user)

It's because it's a nested resource: http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects

Update: As for the form, you can do:

<%= form_for [@user, @friend] do |f| %>
like image 180
clemensp Avatar answered Oct 08 '22 01:10

clemensp