繁体   English   中英

使用simple_form和浅嵌套资源的Rails 4动作路由

[英]Rails 4 action routes with simple_form and shallow nested resources

resources :users, shallow: true do
    resources :shoes
end

这给了我两个不同的创建和编辑路线

user_shoes_path
shoes_path

在我的鞋子_form.html.erb如果我离开表格标签:url作为默认值,当我提交新的或更新的鞋子时,我会收到丢失的路线错误。

如果我提供表单中的url,我可以将其用于新的或编辑更新,但我无法让它同时工作。

这适用于新的:

<%= simple_form_for :shoe, url: user_shoes_path do |f| %>

这适用于编辑,但一旦尝试实际更新就会失败,因为它重定向到/:param_id

<%= simple_form_for :shoe, url: shoes_path(@shoe) do |f| %>

我怎样才能使它适用于两者? 谢谢。

你应该用

<% = simple_form_for [@user, @shoe] do |f| %>

让simple_form做这项工作......

这种情况,如果有@user,简单表单将使用它(对于新的),如果没有(比如编辑),简单表单将不会使用它...

暂无
暂无

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

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