Nested Resources w/ Rails 5.1 form_with

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


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