Nested Resources w/ Rails 5.1 form_with

colincr picture colincr · Oct 24, 2017 · Viewed 9.1k times · Source

Per this pull request I can see that an array should be passed to form_with's model param. However, when I supply the following:

<%= form_with(model: [@trip, @activity], local: true) do |f| %>
  ...
<% end %>

Rails will return - ActionView::Template::Error (undefined method activity_path' for #<#<Class:0x007f80231e3070>:0x007f8023010dd8>):

My routes file looks like:

  resources :trips do
    resources :activities
  end

The output of rake routes looks like -

     trip_activities GET    /trips/:trip_id/activities(.:format)          activities#index
                     POST   /trips/:trip_id/activities(.:format)          activities#create
   new_trip_activity GET    /trips/:trip_id/activities/new(.:format)      activities#new
  edit_trip_activity GET    /trips/:trip_id/activities/:id/edit(.:format) activities#edit
       trip_activity GET    /trips/:trip_id/activities/:id(.:format)      activities#show
                     PATCH  /trips/:trip_id/activities/:id(.:format)      activities#update
                     PUT    /trips/:trip_id/activities/:id(.:format)      activities#update
                     DELETE /trips/:trip_id/activities/:id(.:format)      activities#destroy
               trips GET    /trips(.:format)                              trips#index
                     POST   /trips(.:format)                              trips#create
            new_trip GET    /trips/new(.:format)                          trips#new
           edit_trip GET    /trips/:id/edit(.:format)                     trips#edit
                trip GET    /trips/:id(.:format)                          trips#show
                     PATCH  /trips/:id(.:format)                          trips#update
                     PUT    /trips/:id(.:format)                          trips#update
                     DELETE /trips/:id(.:format)                          trips#destroy

And my activities_controller.rb -

  before_action :set_activity, only: %i[show update edit destroy]

  def edit; end

  def update
    dates = calculate_datetimes(params[:date_range])
    @activity.assign_attributes(name: params[:name],
                                summary: params[:summary],
                                start_datetime: dates[0],
                                end_datetime: dates[1])
    if @activity.save
      flash[:success] = 'Activity successfully updated'
      redirect_to(@trip)
    else
      set_humanized_daterange
      render :edit
    end
  end

  private 

  def set_activity
    @activity = Activity.find(params[:id])
  end

tl;dr - how should I setup my form_with for a nested resource, and why is this form thinking I want to use the activity_path? Ideally I'd like to move this form into a partial and use the same form for both my #new and #edit actions.

Answer

max picture max · Oct 24, 2017

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.