Undefined method `to_sym' for nil:NilClass

Hi,

When updating a ReferralProgram object that has nested Pages in it I
get the error in the subject line.

Rails generates parameters that look like this:

{“pages_attributes”=>{“0”=>{“body”=>“blabla”,
“subject”=>“blabla”,
“id”=>“1”}}}

When the id parameter is removed like this:

{“pages_attributes”=>{“0”=>{“body”=>“blabla”}}}

The exception is not raised. Any ideas on this?

The relevant part that ‘crashes’ the form:

form.html.haml

  • form.inputs :name => I18n.translate(“strings.pages”) do
    • form.semantic_fields_for :pages do |pages|
      %h3= pages.object.title
      %div.page_description= pages.label :description,
      pages.object.description
      = pages.input :subject unless
      pages.object.name.eql?(“thankyou_page”)
      = pages.input :body

Example console session:

ruser@user-laptop:~/rails/1referralprogram$ script/console
Loading development environment (Rails 2.3.8)
h = {“pages_attributes”=>{“0”=>{“body”=>“blabla”,
“subject”=>“blabla”,
“id”=>“1”}}}>> h = {“pages_attributes”=>{“0”=>{“body”=>“blabla”,
?> “subject”=>“blabla”,
?> “id”=>“1”}}}
=> {“pages_attributes”=>{“0”=>{“body”=>“blabla”, “id”=>“1”,
“subject”=>“blabla”}}}

r = ReferralProgram.new(h)
NoMethodError: undefined method to_sym' for nil:NilClass from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:inmethod_missing’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2302:in
expand_hash_conditions_for_aggregates' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2301:ineach’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2301:in
expand_hash_conditions_for_aggregates' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2333:insanitize_sql_hash_for_conditions’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2266:in
sanitize_sql' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1524:inmerge_conditions’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1522:in
each' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1522:inmerge_conditions’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1834:in
add_conditions!' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1717:inconstruct_finder_sql’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1578:in
find_every' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:618:infind’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:638:in
all' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:insend’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:380:in
method_missing_without_paginate' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2178:inwith_scope’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:in
send' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_proxy.rb:207:inwith_scope’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:376:in
method_missing_without_paginate' from /usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.14/lib/will_paginate/finder.rb:170:inmethod_missing’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:349:in
assign_nested_attributes_for_collection_association' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/nested_attributes.rb:244:inpages_attributes=’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in
send' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:inassign_attributes’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in
each' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:inassign_attributes’
from
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in
attributes=' from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2473:ininitialize’
from
/home/user/rails/1referralprogram/app/models/referral_program.rb:34:in
initialize' from (irb):4:innew’
from (irb):4>> h = {“pages_attributes”=>{“0”=>{“body”=>“blabla”}}}
=> {“pages_attributes”=>{“0”=>{“body”=>“blabla”}}}

r = ReferralProgram.new(h)
=> #<ReferralProgram id: nil, name: nil, title: nil, description: nil,
display_url: nil, destination_url: nil, image_file_name: nil,
image_content_type: nil, image_file_size: nil, image_updated_at: nil,
bonus_file_name: nil, bonus_content_type: nil, bonus_file_size: nil,
bonus_updated_at: nil, support_contact_name: nil,
support_email_address: nil, minimum_contacts_per_referral: 1,
minimum_clicks_per_referral: 0, deleted_at: nil, created_at: nil,
updated_at: nil>

I can’t really test this right now but here’s a thought:

  • If you create a new ReferralProgram with new Pages there shouldn’t
    be a Page ID in the params because these Pages don’t exist yet (If you
    assign existing pages double check if a Page with that ID exists).
  • If I remember this correctly multiple nested objects have to be in
    an array in the params like:

“pages_attributes” => [ {“body”=>“blabla”, “subject”=>“blabla”},
{“body” => “blublu”, “subject”=>“blublu”} ]

Hope this helps a little!
Simon

On Sep 14, 5:08 pm, Commander J. [email protected]

I was using the to-csv and fastercsv gem. When I commented them out of
my environment.rb, the error disappeared. I don’t know which gem was
causing it, but not using either of them has fixed it for now. I will
still need CSV export functionality, but now I can at least test if it
doesn’t break nested forms … :slight_smile:

Thanks Simon!

Hi,

I’m facing the exact same error right now.
Is there a solution to this issue?

I’d really like to use to-csv+fastercsv.

Let me know.

THX alot,

Thomas

Commander J. wrote:


View this message in context:
http://old.nabble.com/undefined-method-`to_sym'-for-nil%3ANilClass-tp29709580p30776583.html
Sent from the RubyOnRails Users mailing list archive at Nabble.com.