Multiple in one form

hi,

I just try to deal with multiples model in one form,
using ajax to add models. but got a weird params hash.

got:
:master_model => {
:sub_model => [
{ :field_1 => “1” },
{ :field_1 => “2”, :field_2 => “3” },
{ :field_2 => “4” }
]
}

instead of :

:master_model => {
:sub_model => [
{ :field_1 => “1” :field_2 => “3” },
{ :field_1 => “2”, :field_2 => “4” }
]
}

anybody can help me?

i just checked it again and it looks like a form_remote_for bug,
because with a non ajax form_for params are well formed,
can someone confirm it?

On Sep 22, 1:29 am, jney [email protected] wrote:

i just checked it again and it looks like a form_remote_for bug,
because with a non ajax form_for params are well formed,
can someone confirm it?

It’s impossible to confirm with the little information you’ve given.
Care to elaborate?

Hi erol,

Try this :

<% form_for(@master_model) do |f| %>
<%= f.error_messages %>

<%- fields_for(‘master_model[sub_model][]’, SubModel.new) do |sm| -
%>
<%= sm.text_field :field1 %>
<%= sm.text_field :field2 %>
<%= sm.text_field :field3 %>
<%- end -%>

<%- fields_for(‘master_model[sub_model][]’, SubModel.new) do |sm| -
%>
<%= sm.text_field :field1 %>
<%= sm.text_field :field2 %>
<%= sm.text_field :field3 %>
<%- end -%>

<%= f.submit "Create" %>

<% end %>

then check params format (it should be ok), then replace “form_for”
with “remote_form_for” and check params format,
“master_model[sub_model]” should be wrong

I am having this same problem, but I need to do this with ajax…
any ideas? I’ve been racking my brain on this for a few hours now and
am losing it,

On Thu, 2008-09-25 at 12:11 -0700, Nathan R wrote:

<%= f.error_messages %>

<%-fields_for(‘master_model[sub_model][]’, SubModel.new) do |sm| -
%>
<%= sm.text_field :field1 %>
<%= sm.text_field :field2 %>
<%= sm.text_field :field3 %>
<%- end -%>

I find way better results with
form_remote_for([@master_model, @sub_model])

in cases where I need several instances of sub model
I do something like this in the view

<% unless @master_model.sub_models.nil? %>
<%= render :partial => ‘sub_model’, :collection =>
@master_model.sub_models, :object => @master_model%>
<% end %>
<%= render :partial => ‘new_sub_model’, :object => [@master_model,
@sub_model] %>

then the partials use
div_for(sub_model) so the controller can talk back ajax to each instance
of the form

the new_sub_model partial sends to create,
then create via rjs, refreshes the list of sub_models and shows a new
empty new_sub_model template