Ruby on Rails Monday, May 3, 2010

Hello,
I think the issue could be in the 'Response' attribute in :

accepts_nested_attributes_for :response_items, :allow_destroy =>
true,
:reject_if => proc { |attributes| attributes['Response'].blank? }

The way you wrote this method means that creations (or updates) of
ResponseItem instances are being rejected if the attribute named
'Response' is blank. I don't think ResponseItem has a 'Response'
attribute, which by the way is an invalid name (capitalized) for an
attribute name.

If you correct the statement above with this:

accepts_nested_attributes_for :response_items, :allow_destroy =>
true,
:reject_if => proc { |attributes| attributes.all? {|k,v|
v.blank?} }

ResponseItem instances will be rejected (not saved) when all
attributes are blank.
If you need to reject instances when a specific attribute is blank you
could substitute the 'proc' with :

proc { |attributes| attributes['attribute_name'].blank? }


On May 2, 11:27 pm, Kevin Tambascio <li...@ruby-forum.com> wrote:
> Hi,
>
> I've been struggling with getting accepts_nested_attributes_for to work
> perfectly with a nested model form.  This is a project that was started
> before Rails 2.0, and I'm trying to use new techniques in the new
> functionality.  I'm presently using Rails 2.3.4.  I'm down to one last
> issue, and I'm not finding an answer so far.
>
> The system I'm building deals with Items that have ResponseItem's as
> children (think questions with multiple choices). Creating a new item
> works fine.  By default, the item has 12 potential responses.  A form is
> rendered that allows the user to enter up to 12 responses.  The saving
> of the new item works fine.
>
> The issue is with editing.  Say the item was created with 4 responses.
> When the edit form is rendered, 4 of the responses are shown, and the
> other 8 are blank (only the first 4 are representing objects in the
> databases, the blank ones have no database records).  When the form is
> submitted, I'm receiving an HTTP 500 error, with this output in the log:
>
> Completed in 176ms (View: 95, DB: 22) | 200 OK
> [http://localhost/items/edit/648?assessment_id=1]
> /!\ FAILSAFE /!\  Sun May 02 21:04:01 UTC 2010
>   Status: 500 Internal Server Error
>   expected Array (got Hash) for param `response_items_attributes'
>     /opt/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:85:in
> `normalize_params'
>     /opt/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:94:in
> `normalize_params'
>     /opt/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:62:in
> `parse_nested_query'
>     /opt/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:60:in
> `each'
>     /opt/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:60:in
> `parse_nested_query'
>     /opt/jruby-1.4.0/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/request.rb:140: in
> `POST'
> ....
>
> The call stack does not even show my code, it's all framework so far.
>
> The models look like this:
>
> class Item < ActiveRecord::base
>   ...
>   attr_accessor :response_item_ids
>   has_many :response_items
>   accepts_nested_attributes_for :response_items, :allow_destroy => true,
>     :reject_if => proc { |attributes| attributes['Response'].blank? }
>   ...
> end
>
> class ResponseItem < ActiveRecord::base
>   ...
>   belongs_to :item
>   ...
> end
>
> The only real clue so far, is the names of the fields on the edit form
> are different whether it's a pre-filled response, or a blank one.
>
> This is for a response that has an item in the database:
> <td>
>     <input id="item_response_items_attributes_1311_Response"
> name="item[response_items_attributes][1311][Response]" size="30"
> type="text" value="Very familiar - I could explain common
> product/service offerings" />
>   </td>
>
> This is for a blank response item, with no object in the database:
> <td>
>     <input id="item_response_items_attributes__Response"
> name="item[response_items_attributes][][Response]" size="30" type="text"
> />
>   </td>
>
> My hunch is that mixing a form with some items that have an ID, and some
> that are blank for an ID, is potentially causing trouble in the
> normalize_params function?
>
> One possible solution could be that for blank responses, I create a
> temporary database item, so that they have an ID field filled out.  When
> I try to edit an item with all 12 responses, it does work because
> there's an ID field for each response.
>
> Clear as mud?  :)  Tough to explain this in a forum.  If anyone has any
> thoughts, I'd appreciate it.
>
> Thanks,
> Kevin
> --
> Posted viahttp://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment