30

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.

1 Answer 1

47

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| %>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Maybe I'm misunderstanding about where to put the path.. When I do form_for user_friend_path(@friend) it throws up an even stranger error. All I'm trying to do is display a form to add "friends" and associate them as belonging to the user.
Ah, I see what you're trying to do. You have to do form_for [@user, @friend]
Thanks so much for your input. That did it! Really appreciate the help as I'm just beginning. Happy Holidays to you sir.
I've been bashing my brains out trying to get this working. Many, many thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.