Nested Resources w/ Rails 5.1 form_with

别来无恙 提交于 2019-11-29 05:36:21

Try specifying the model and url separately:

form_with(model: @activity, url: [@trip, @activity])

According the docs the the values for url are "Akin to values passed to url_for or link_to" so using an array should work.

This also works with shallow nesting since the array is compacted.

You just need to add the url to the route you want the form to point to, ie: , url: trip_activity_path or whatever the path you are hitting is

Adding to max answer, if you want to support shallow nesting you can also use:

form_with(model: [@trip, @activity])

And change the render on your edit.html.erb to pass nil to trip, like:

<%= render 'form', trip: nil, activity: @activity %>

and keep it passing the trip model in your new.html.erb

Changing the url is fine, but you can instead change the model which allows the builder to infer the url and any scoping etc:

<%= form_with(model: child.new_record? ? [@parent, child] : child, local: true) do |form| %>

new/edit views: <%= render 'form', child: @child %>

controllers will need to set @parent from url params, which is convention for nested resources anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!