6
votes

I am trying to use a nested form but keep getting this error when saving -

uninitialized constant User::Userplan

Here is my model code -

User

class User < ActiveRecord::Base 
  has_many :userplans
  has_many :plans, :through => :userplans
  accepts_nested_attributes_for :userplans

UserPlan

class UserPlan < ActiveRecord::Base
 belongs_to :plan
 belongs_to :user

Plan

class Plan < ActiveRecord::Base
 has_many :userplans
 has_many :users, :through => :userplans

Form

<%= form_for(@user) do |f| %>
 <%= hidden_field_tag "user[userplans][plan_id]", 2 %>
 <%= f.text_field :first_name %></p>

I know using a hidden_field_tag is wrong but im not sure of a better way

Thanks for any help!

activerecord (3.0.3) lib/active_record/associations/association_proxy.rb:260:in raise_on_type_mismatch' activerecord (3.0.3) lib/active_record/associations/association_collection.rb:352:inblock in replace' activerecord (3.0.3) lib/active_record/associations/association_collection.rb:352:in each' activerecord (3.0.3) lib/active_record/associations/association_collection.rb:352:inreplace' activerecord (3.0.3) lib/active_record/associations.rb:1524:in block in collection_accessor_methods' activerecord (3.0.3) lib/active_record/base.rb:1559:inblock in attributes=' activerecord (3.0.3) lib/active_record/base.rb:1555:in each' activerecord (3.0.3) lib/active_record/base.rb:1555:inattributes=' cancan (1.4.1) lib/cancan/controller_resource.rb:72:in build_resource' cancan (1.4.1) lib/cancan/controller_resource.rb:48:inload_resource_instance' cancan (1.4.1) lib/cancan/controller_resource.rb:30:in load_resource' cancan (1.4.1) lib/cancan/controller_resource.rb:24:inload_and_authorize_resource' cancan (1.4.1) lib/cancan/controller_resource.rb:9:in block in add_before_filter' activesupport (3.0.3) lib/active_support/callbacks.rb:436:inrun_3988671192319902872__process_action_982103756142273942_callbacks' activesupport (3.0.3) lib/active_support/callbacks.rb:409:in _run_process_action_callbacks' activesupport (3.0.3) lib/active_support/callbacks.rb:93:inrun_callbacks' actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in process_action' actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:inblock in process_action' activesupport (3.0.3) lib/active_support/notifications.rb:52:in block in instrument' activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:ininstrument' activesupport (3.0.3) lib/active_support/notifications.rb:52:in instrument' actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:29:inprocess_action' actionpack (3.0.3) lib/action_controller/metal/rescue.rb:17:in process_action' actionpack (3.0.3) lib/abstract_controller/base.rb:120:inprocess' actionpack (3.0.3) lib/abstract_controller/rendering.rb:40:in process' actionpack (3.0.3) lib/action_controller/metal.rb:138:indispatch' actionpack (3.0.3) lib/action_controller/metal/rack_delegation.rb:14:in dispatch' actionpack (3.0.3) lib/action_controller/metal.rb:178:inblock in action' actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in call' actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:indispatch' actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:27:in call' rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:inblock in call' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in block in recognize' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:75:inoptimized_each' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in recognize' rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:incall' actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:492:in call' warden (1.0.2) lib/warden/manager.rb:35:inblock in call' warden (1.0.2) lib/warden/manager.rb:34:in catch' warden (1.0.2) lib/warden/manager.rb:34:incall' actionpack (3.0.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in call' actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:incall' rack (1.2.1) lib/rack/methodoverride.rb:24:in call' actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:incall' actionpack (3.0.3) lib/action_dispatch/middleware/flash.rb:182:in call' actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:incall' actionpack (3.0.3) lib/action_dispatch/middleware/cookies.rb:295:in call' activerecord (3.0.3) lib/active_record/query_cache.rb:32:inblock in call' activerecord (3.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in cache' activerecord (3.0.3) lib/active_record/query_cache.rb:12:incache' activerecord (3.0.3) lib/active_record/query_cache.rb:31:in call' activerecord (3.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:353:incall' actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:46:in block in call' activesupport (3.0.3) lib/active_support/callbacks.rb:415:in_run_call_callbacks' actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:44:in call' rack (1.2.1) lib/rack/sendfile.rb:107:incall' actionpack (3.0.3) lib/action_dispatch/middleware/remote_ip.rb:48:in call' actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:incall' railties (3.0.3) lib/rails/rack/logger.rb:13:in call' rack (1.2.1) lib/rack/runtime.rb:17:incall' activesupport (3.0.3) lib/active_support/cache/strategy/local_cache.rb:72:in call' rack (1.2.1) lib/rack/lock.rb:11:inblock in call' :10:in synchronize' rack (1.2.1) lib/rack/lock.rb:11:incall' actionpack (3.0.3) lib/action_dispatch/middleware/static.rb:30:in call' railties (3.0.3) lib/rails/application.rb:168:incall' railties (3.0.3) lib/rails/application.rb:77:in method_missing' railties (3.0.3) lib/rails/rack/log_tailer.rb:14:incall' rack (1.2.1) lib/rack/content_length.rb:13:in call' rack (1.2.1) lib/rack/handler/webrick.rb:52:inservice' /Users/joshcrowder/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in service' /Users/joshcrowder/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:inrun' /Users/joshcrowder/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

1

1 Answers

2
votes

I think your issue is with the naming of your associations. Since your UserPlan model is CamelCased <- like that, Rails 'tabelizes' the name of it into :user_plans, not :userplan. Your error, as you can see, stems from Rails not being able to find Userplan, when it should be looking for UserPlan.

User:

class User < ActiveRecord::Base 
  has_many :user_plans
  has_many :plans, :through => :user_plans
  accepts_nested_attributes_for :user_plans

Plan:

class Plan < ActiveRecord::Base
  has_many :user_plans
  has_many :users, :through => :user_plans

View:

<%= form_for(@user) do |f| %>
 <%= hidden_field_tag "user[user_plans][plan_id]", 2 %>
 <%= f.text_field :first_name %></p>

With regards to the hidden_field_tag, what exactly are you trying to do? Perhaps you should open another question?

Hope this helps!