嵌套资源的控制器路径 - 未定义的方法 `<controller>_path' [英] Controller path for nested resource - undefined method `<controller>_path'

查看:33
本文介绍了嵌套资源的控制器路径 - 未定义的方法 `<controller>_path'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在/users/2/friends/new 中显示我的表单.我收到了

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

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

这是表格的开头

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

和朋友控制器

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

这是路线

resources :users do
       resources :friends
end

以及来自rake routes"的相关路径

And the relevant path from "rake routes"

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

非常感谢任何帮助或见解.这是我的第一个 rails 3 应用程序.

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

推荐答案

尝试:

user_friends_path(@user)

这是因为它是一个嵌套资源:http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects

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

这篇关于嵌套资源的控制器路径 - 未定义的方法 `&lt;controller&gt;_path'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆