简体   繁体   English

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

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

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

This gives me two different routes for create and edit 这给了我两个不同的创建和编辑路线

user_shoes_path
shoes_path

In my shoes _form.html.erb if I leave the form tag :url as default I get a missing routes error when I submit a new or updated shoe. 在我的鞋子_form.html.erb如果我离开表格标签:url作为默认值,当我提交新的或更新的鞋子时,我会收到丢失的路线错误。

If I supply the url in the form I can get it to work for either the new or the edit update, but I can't get it to work for both. 如果我提供表单中的url,我可以将其用于新的或编辑更新,但我无法让它同时工作。

This works for the new: 这适用于新的:

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

This works for the edit, but will fail once it tries the actual update since it redirects to /:param_id : 这适用于编辑,但一旦尝试实际更新就会失败,因为它重定向到/:param_id

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

How can I make it work for both? 我怎样才能使它适用于两者? Thanks. 谢谢。

You should use 你应该用

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

and let do simple_form do the work... 让simple_form做这项工作......

This case, if there is a @user, simple form will use it (as for a new), if there isn't (like for an edit), simple form won't use it... 这种情况,如果有@user,简单表单将使用它(对于新的),如果没有(比如编辑),简单表单将不会使用它...

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

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