Rails nested resources and form_for

Ah ha! I was working with Rory creating a Rails form backed by a nested resource the other day and we had code like:

<% form_for(:participant, @participant, :url => poll_participant_path(@poll, @participant), :html => {:method => :put}) do |f| %>

#...

<% end %>

That code smelled really bad, having to put in the :url and the :html felt really wrong.

Solution:

<% form_for([@poll, @participant]) do |f| %>

#…

<% end %>

The array of objects makes this much cleaner.

Leave a Reply