Rails 4 shallow routes resource form submission no

2019-09-09 16:13发布

I've got shallow nested resource routes like below:

    resources :venues, shallow: true do
        #Halls
        get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
        get "hall/:id/visit" => "halls#visit", as: :hall_visit
        get "structure", :to => "venues#venue_structure"
        resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
        resources :halls do
            resources :webcasts
            resources :booths do
                resources :chats
            end
        end
    end

Below is a simple_form that is currently being used.

= simple_form_for(hall_booths_path(@booth), :html => { :class => "form-horizontal" }, :wrapper => "horizontal", defaults: { :input_html => { class: "form-control"}, label_html: { class: "col-lg-4" } } ) do |f|
  = f.simple_fields_for @booth do |b|

The problem is that hall_booths_path(@booth) part is generating /halls/1/booths/new instead of /halls/1/booths

Is there something wrong here that needs fixing?

my booths routes:

hall_booths_path     GET     /halls/:hall_id/booths(.:format)    booths#index
                     POST    /halls/:hall_id/booths(.:format)    booths#create
new_hall_booth_path  GET     /halls/:hall_id/booths/new(.:format)    booths#new
edit_booth_path      GET     /booths/:id/edit(.:format)  booths#edit
booth_path           GET     /booths/:id(.:format)   booths#show
                     PATCH   /booths/:id(.:format)   booths#update
                     PUT     /booths/:id(.:format)   booths#update
                     DELETE  /booths/:id(.:format)   booths#destroy

1条回答
Root(大扎)
2楼-- · 2019-09-09 17:06

Pass the path in the options hash, not as the first argument:

<%= simple_form_for :booth, :url => hall_booths_path(@hall) do |f| %>
    ...
<% end %>

Note that the argument to hall_booths_path is a Hall, not a Booth. When you create a child, you need to supply the parent.

Another option is to pass not the URL but the model objects. Assuming @hall is an existing Hall and @booth is a new Booth:

<%= simple_form_for [@hall, @booth] do %>
    ...
<% end %>

I find this approach much simpler.

查看更多
登录 后发表回答