Rails 4浅路线资源表单提交无效

时间:2014-01-20 11:59:38

标签: ruby-on-rails ruby ruby-on-rails-4

我有如下的浅层嵌套资源路径:

    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

以下是当前正在使用的simple_form。

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

问题是hall_booths_path(@booth)部分正在生成/halls/1/booths/new而不是/halls/1/booths

这里有什么问题需要修理吗?

我的booths路线:

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 个答案:

答案 0 :(得分:1)

在选项哈希中传递路径,而不是第一个参数:

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

请注意,hall_booths_path的参数是Hall,而不是Booth。创建子项时,需要提供父项。

另一个选择是不传递URL而是传递模型对象。假设@hall是现有的Hall@booth是新的Booth

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

我发现这种方法更简单。