Hey, I have a bunch of forms for different controllers all on the same page. This is because theres a bunch of models which stack on each other and this is the best way to edit them.
I'm using partials to include each form into the view, passing locals for the object and url params to be used by form_for
The first (top level) form partial:
- form_for :person, local_assigns[:obj], :url => local_assigns[:url] do |person_form|
= person_form.error_messages
%p
= person_form.label :first_name
= person_form.text_field :first_name
%p
= person_form.label :last_name
= person_form.text_field :last_name
%p
= person_form.submit 'Save'
- if local_assigns[:obj]
= link_to 'Destroy', local_assigns[:obj], :confirm => 'Are you sure?', :method => :delete
which is included into the view via
= render :partial => 'person_form', :locals => { :url => { :controller => 'people', :action => 'create' }, :obj => @person }
to create a new Person, and
= render :partial => 'person_form', :locals => { :url => "people/update/#{person.id}", :obj => person }
within this block:
- Person.all.each do |person|
to edit all existing people.
My problem is that every form, excluding the first (top level) person create and edit forms, the action gets set to the current controller.
The form_for partials for all other forms are very similar:
- form_for :day, local_assigns[:obj], local_assigns[:url] do |day_form|
= day_form.error_messages
%p
= day_form.label :effective_date, 'Date'
= day_form.datetime_select :effective_date
= day_form.label :person_id
= day_form.collection_select(:person_id, Person.all, :id, :full_name, { :selected => local_assigns[:ref] ? local_assigns[:ref].id : 1 } )
%p
= day_form.submit 'Save'
- if local_assigns[:obj]
= link_to 'Destroy', local_assigns[:obj], :confirm => 'Are you sure?', :method => :delete
included via
= render :partial => 'day_form', :locals => { :url => { :controller => 'days', :action => 'create' }, :obj => @day, :ref => person }
for the create form, and
= render :partial => 'day_form', :locals => { :url => "days/update/#{day.id}", :obj => day, :ref => person }
for the edit forms.
The two models, Person and Day, are linked together by Person having many :days and Day belonging to :person. The associations are all setup correctly and working, the problem is with rendering all the forms with different controllers to generate the correct action url.
Any suggestions?
(I'm using haml btw)