开发者

Rails 3 nested resource route problem as form_for

I have nested resources like this in my routes.rb - (my rake:routes gist)

namespace(:admin) do 
  resources :restaurants do
    resources :menus
    resources :menu_items
  end
end

In the controller:

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

Trying to create a new MenuItem (action #new), by the url: http://127.0.0.1:3001/admin/restaurants/1/menu_items/new I get the error:

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

How do I make this form work? It was created out of a nifty:scaffold

UPDATE

I also tried this in the _form:

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

But ended with a similar error:

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

undefined metho开发者_开发百科d `restaurant_admin_menu_items_path' for #<#<Class:0xb68162b0>:0xb6813dd0>
Extracted source (around line #1):

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

Should I file a bug?


form_for([@restaurant, @menu_item])


I think the problem is in the form. This worked for me:

<%= form_for(@menu_items, :url => restaurant_menu_items_path(@menu_items.restaurant)) do |f| %>


I'm having the same issue. The only solution I have found is to pass a url to the form_for.

<% url = (action_name == "new" ? {:action=>"create", :controller=>"admin/menu_item"} : {:action=>"update", :controller=>"admin/menu_item"})%>

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

One additional note, you will not get params[:menu_item] back, instead you will see params[:admin_menu_item].

Hope that helps you out!


You can look up your routes by running on the command line.

rake routes

It looks like you're calling your routes incorrectly.

Array notation would be:

form_for([:admin, @restaurant, @menu_item])

And the named route for create:

admin_restaurant_menu_items_path(@restaurant)


Dealing with nested resources and namespaces is a Vietnam (pita).

Here is my nasty solution:

= form_for @admin_menu_item, 
    :url => (@admin_menu_item.try(:new_record?) ? 
        admin_restaurant_menu_items_path(@admin_restaurant) : 
        admin_menu_item_path(@ admin_menu_item)) do |f|
    ...

I hope you can help.


The only solution that worked for me correctly (for both new and edit resource) was:

form_for @menu_item, :url => url_for([:admin, @restaurant, @menu_item])


I'm using Rails 5 (not that I imagine it matters) and this worked for me:

= simple_form_for [:admin, @restaurant, @menu_item] do |f|

The fact that it's simple_form_for rather than form_for probably doesn't matter either.

Funnily enough, I'm building an app with the same exact resource names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜