How do I build an array of objects from my params?

Hi,

I have a form model which has_many form_item objects. How do I build
a new form model from these objects? This call

                @form = Form.new(:user_id => session[:user_id],
                                 :form_items => params[:form_items])

is giving me the error, "ActiveRecord::AssociationTypeMismatch in
FormsController#create -- FormItem expected, got
HashWithIndifferentAccess". request passed in is

{"commit"=>"Submit Form",
"form_items"=>[{"description"=>"aaa",
"prescription_number"=>"111",
"day_supply"=>"234"}]}

Below is the partial I'm using to collect form_item data:

<% fields_for "form[form_item_attributes][]", form_item do |
form_item_form| %>
<div class="form_item">
<% @form_item = form_item %>
<%= error_messages_for :form_item %>
... table data
           <td align="center"><%= text_field_tag "form_items[]
[prescription_number]", form_item.prescription_number, "size" => 15
%></td>
           <td align="center"><%= text_field_tag
"form_items[#{form_item.id}][description]", form_item.description,
"size" => 15 %></td>
           <td align="center"><%= text_field_tag
"form_items[#{form_item.id}][day_supply]", form_item.day_supply,
"size" => 15 %></td>
... more table data
<% end %>

Thanks for your help, - Dave

Hi,

I have a form model which has_many form_item objects. How do I build
a new form model from these objects? This call

               @form = Form.new(:user_id => session[:user_id],
                                :form_items => params[:form_items])

is giving me the error, "ActiveRecord::AssociationTypeMismatch in
FormsController#create -- FormItem expected, got
HashWithIndifferentAccess". request passed in is

Right now, you're going to have to iterate over params[:form_items]
creating activerecord objects as you go.

Fred

You posted this same thing on the rails forum. Please do not cross post.

Maybe he wasn't getting an answer? I don't frequent the rails forum
and had I known the answer the only way I could have helped would have
been answering this post.

Sorry about that. You were perfectly clear, don't know why I missed
previous posting. Anyway this is the code I used that worked for
me ...

                form_items_arr = Array.new
                for form_item in params[:form_items]
                        form_item_obj = FormItem.new(form_item)
                        form_items_arr.push(form_item_obj)
                end
                @form = Form.new(:user_id => session[:user_id],
                                 :form_items => form_items_arr)

All the best, - Dave

I'm getting an internal server error in my logs, saying

  Status: 500 Internal Server Error
  Conflicting types for parameter containers. Expected an instance of
Hash but found an instance of Array. This can be caused by colliding
Array and Hash parameters like qs[]=value&qs[key]=value.

Hi, you should be able to do the following:

form_items_arr = Array.new( params[:form_items] )
@form = Form.new(:user_id => session[:user_id],

:form_items => form_items_arr)

Good luck,

-Conrad

Sorry about that. You were perfectly clear, don't know why I missed
previous posting. Anyway this is the code I used that worked for
me ...

               form_items_arr = Array.new
               for form_item in params[:form_items]
                       form_item_obj = FormItem.new(form_item)
                       form_items_arr.push(form_item_obj)
               end

You can be slightly more concise: form_items_arr =
params[:form_items].collect {|attrs| FormItem.new attrs}

Fred