Rails 4 浅层路线资源表单提交不起作用 [英] Rails 4 shallow routes resource form submission not working

查看:42
本文介绍了Rails 4 浅层路线资源表单提交不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面是一个目前正在使用的 simple_form.

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|

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

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?

我的摊位路线:

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

推荐答案

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

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

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

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

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.

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

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.

这篇关于Rails 4 浅层路线资源表单提交不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆