我在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


尝试通过URL创建一个新的MenuItem(操作#new):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| %>
...


如何使此表格生效?它是由一个漂亮的:scaffold创建的

更新

我也在_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| %>

10-04 12:06