Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

ruby on rails 3 - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...