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| %>

<% @form_item = form_item %> <%= error_messages_for :form_item %> ... table data <%= text_field_tag "form_items[] [prescription_number]", form_item.prescription_number, "size" => 15 %> <%= text_field_tag "form_items[#{form_item.id}][description]", form_item.description, "size" => 15 %> <%= text_field_tag "form_items[#{form_item.id}][day_supply]", form_item.day_supply, "size" => 15 %> ... more table data <% end %>

Thanks for your help, - Dave

On 7 Aug 2008, at 18:59, [email protected] wrote:

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

On Mon, Aug 11, 2008 at 12:06 PM, [email protected] <

On 11 Aug 2008, at 20:06, [email protected] wrote:

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