繁体   English   中英

我应该如何设置此表单以提交到正确的路径?

[英]How should I set up this form to submit to the correct path?

我的产品有很多变体。 当我编辑和提交变体时,我需要它来保持与产品的连接。 所以 URl 将是 localhost/products/1/variants/3/。 它现在的方式是提交到 localhost/variants/3 并因为找不到产品而出错?

<%= form_with(model: variant, local: true) do |form| %>

编辑。 路线:

  resources :variants
  resources :products do
    resources :option_values
    resources :option_value_variants
    resources :variants do
        collection do
          post :update_positions
        end
      end
  end

当您编辑和提交变体时,您不需要创建嵌套路由。 product_id 已经与变体一起保存(从创建时开始)。 所以需要使用嵌套路由——你只需要嵌套路由来newcreate操作,这就是所谓的“浅嵌套”。 因为只有在这两个操作中,您才能通过 url(参数)以外的任何其他方式访问产品 ID。

您的代码中的错误可能来自您创建了一个嵌套路由但您没有将产品实例提供给表单的事实:

%= form_with(model: [@product, @variant], local: true) do |form| %>

但如上所述,这里不需要,只需相应地更改您的路线即可。

更新问题后:

因此,对于浅层路由,您的路由应如下所示:

resources :variants, only: [:index, :show, :edit, :update, :destroy]
  resources :products do
    ...
    resources :variants, only [:create, :new] do
        collection do
          post :update_positions
        end
      end
  end

只有createnew需要嵌套,其他路由不需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM