11

I'm using fields_for in my form like so

<%= form_for @user %>
  ...
  <%= f.fields_for :photos do |f2| %>
    <%= f2.radio_button :public, 'true' %>
  <% end %>
  ...
<% end %>

Here are the radio buttons it generates:

<input id="user_photos_attributes_0_public_true" name="user[photos_attributes][0][public]" type="radio" value="true" /> 
<input id="user_photos_attributes_0_id" name="user[photos_attributes][0][id]" type="hidden" value="1" /> 

<input id="user_photos_attributes_1_public_true" name="user[photos_attributes][1][public]" type="radio" value="true" /> 
<input id="user_photos_attributes_1_id" name="user[photos_attributes][1][id]" type="hidden" value="4" /> 

<input id="user_photos_attributes_2_public_true" name="user[photos_attributes][2][public]" type="radio" value="true" /> 
<input id="user_photos_attributes_2_id" name="user[photos_attributes][2][id]" type="hidden" value="5" />
...

I have this in user.rb

  has_many :photos
  accepts_nested_attributes_for :photos

When form is submitted I get this error:

Error during failsafe response: ActionView::Template::Error
TypeError (expected Hash (got Array) for param `photos_attributes'):

Does anyone know why this is happening?

Btw, I'm using Rails 3.0.0.rc2

2 Answers 2

2

How are you saving your model?

If you inspect the params hash, you will get something like:

{ :user => {:photo_attributes => [{:id => 1, :public => true}, {:id => 4, :public => false}] }, :your_other_params => ... }}

So a User.new(params[:user]).save should work. Unless you are passing each hash of attributes instead of the array. See this article if you need a more in-depth detail.

What is in your params hash? That'd help you to trace the problem.

BTW, if you want a "true/false" behavior (I assume that because of the is_public property), rather than "present/non-present", a checkbox should be used. Radio buttons are for mutually exclusive options.

Sign up to request clarification or add additional context in comments.

5 Comments

Save occurs with standard @user.update_attributes(params[:user]). Unfortunately I can't view the params hash because it dies before printing that to the console. I assume the params hash has the array like you wrote it though - so still not sure why it's complaining about that. Thanks for the response!
@Brian, you can do logger.debug(params.inspect) to check.
Hi Andrew, thanks for the response. So actually where would I put that line? It dies before even entering my controller action, or application_controller. It's some sort of failsafe for rails that dies while first processing the request so it never even enters any of my code. Or is there somewhere else to add it?
I'm having the same problem! Has anyone figured this out yet?
Because it's dying in Rack, you won't be able to debug at the rails layer. to help with debugging the issue, I've done this in the past: Using the js console, serialize the form, and then parse it manually. Then post here what the params are.
1

I recently had the same problem. Instead of trying to get the params through the controller, we used the Chrome tools to see what was being passed in the params and found that we were passing an empty hash/array e.g. params[] versus params[:something]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.