ruby-on-rails-3 – Rails 3嵌套资源路由问题为form_for

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – Rails 3嵌套资源路由问题为form_for前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的routes.rb中嵌套了这样的资源 – ( my rake:routes gist)
namespace(:admin) do 
  resources :restaurants do
    resources :menus
    resources :menu_items
  end
end

在控制器中:

def new 
  @restaurant = Restaurant.find(params[:restaurant_id])
  @menu_item =  @restaurant.menu_items.build
end

尝试创建一个新的MenuItem(动作#new),由url:http://127.0.0.1:3001/admin/restaurants/1/menu_items/new我得到错误

NoMethodError in Admin/menu_items#new

Showing /home/fps/workspace3/peded/app/views/admin/menu_items/_form.html.erb where line #1 raised:

undefined method `admin_menu_items_path' for #<#<Class:0xb6582d78>:0xb6581f2c>
Extracted source (around line #1):

1: <%= form_for @menu_item do |f| %>
...

如何使此表单正常工作?它是由一个漂亮的:脚手架创建的

UPDATE

我也在_form中尝试过:

<%= form_for [:restaurant,@menu_item] do |f| %>

但结束了类似的错误

Showing /home/fps/workspace3/peded/app/views/admin/menu_items/_form.html.erb where line #1 raised:

undefined method `restaurant_admin_menu_items_path' for #<#<Class:0xb68162b0>:0xb6813dd0>
Extracted source (around line #1):

1: <%= form_for [:restaurant,@menu_item] do |f| %

我应该提交错误吗?

解决方法

我认为问题是形式.这对我有用:
<%= form_for(@menu_items,:url => restaurant_menu_items_path(@menu_items.restaurant)) do |f| %>

猜你在找的Ruby相关文章