Rails 3 - has_many through with nested form
我正在尝试使用嵌套表单,但在保存时不断收到此错误 -
1 | uninitialized constant User::Userplan |
这是我的型号代码 -
用户
1 2 3 4 | class User < ActiveRecord::Base has_many :userplans has_many :plans, :through => :userplans accepts_nested_attributes_for :userplans |
UserPlan
1 2 3 | class UserPlan < ActiveRecord::Base belongs_to :plan belongs_to :user |
计划
1 2 3 | class Plan < ActiveRecord::Base has_many :userplans has_many :users, :through => :userplans |
形成
1 2 3 4 | <%= form_for(@user) do |f| %> <%= hidden_field_tag"user[userplans][plan_id]", 2 %> <%= f.text_field :first_name %> </p> |
我知道使用hidden_??field_tag是错误的,但我不确定更好的方法
谢谢你的帮助!
activerecord(3.0.3)lib / active_record / associations / association_proxy.rb:260:在
activerecord (3.0.3) lib/active_record/associations/association_collection.rb:352:in
activerecord(3.0.3)lib / active_record / associations / association_collection.rb:352:in
activerecord (3.0.3) lib/active_record/associations/association_collection.rb:352:in
activerecord(3.0.3)lib / active_record / associations.rb:1524:in
activerecord (3.0.3) lib/active_record/base.rb:1559:in
activerecord(3.0.3)lib / active_record / base.rb:1555:in
activerecord (3.0.3) lib/active_record/base.rb:1555:in
cancan(1.4.1)lib / cancan / controller_resource.rb:72:in
cancan (1.4.1) lib/cancan/controller_resource.rb:48:in
cancan(1.4.1)lib / cancan / controller_resource.rb:30:in
cancan (1.4.1) lib/cancan/controller_resource.rb:24:in
cancan(1.4.1)lib / cancan / controller_resource.rb:9:in
activesupport (3.0.3) lib/active_support/callbacks.rb:436:in
activesupport(3.0.3)lib / active_support / callbacks.rb:409:in
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in
actionpack(3.0.3)lib / abstract_controller / callbacks.rb:17:在process_action中的
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in
activesupport(3.0.3)lib / active_support / notifications.rb:52:in
activesupport (3.0.3) lib/active_support/notifications/instrumenter.rb:21:in
activesupport(3.0.3)lib / active_support / notifications.rb:52:in
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:29:in
actionpack(3.0.3)lib / action_controller / metal / rescue.rb:17:in
actionpack (3.0.3) lib/abstract_controller/base.rb:120:in
actionpack(3.0.3)lib / abstract_controller / rendering.rb:40:in
actionpack (3.0.3) lib/action_controller/metal.rb:138:in
actionpack(3.0.3)lib / action_controller / metal / rack_delegation.rb:14:in
actionpack (3.0.3) lib/action_controller/metal.rb:178:in
actionpack(3.0.3)lib / action_dispatch / routing / route_set.rb:62:in
actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in
actionpack(3.0.3)lib / action_dispatch / routing / route_set.rb:27:in
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in
rack-mount(0.6.13)lib / rack / mount / code_generation.rb:93:in
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:75:in
rack-mount(0.6.13)lib / rack / mount / code_generation.rb:92:in
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in
actionpack(3.0.3)lib / action_dispatch / routing / route_set.rb:492:在
warden (1.0.2) lib/warden/manager.rb:35:in
warden(1.0.2)lib / warden / manager.rb:34:in
warden (1.0.2) lib/warden/manager.rb:34:in
actionpack(3.0.3)lib / action_dispatch / middleware / best_standards_support.rb:17:in
actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:in
rack(1.2.1)lib / rack / methodoverride.rb:24:in
actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:in
actionpack(3.0.3)lib / action_dispatch / middleware / flash.rb:182:in
actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:in
actionpack(3.0.3)lib / action_dispatch / middleware / cookies.rb:295:在
activerecord (3.0.3) lib/active_record/query_cache.rb:32:in
activerecord(3.0.3)lib / active_record / connection_adapters / abstract / query_cache.rb:28:in
activerecord (3.0.3) lib/active_record/query_cache.rb:12:in
activerecord(3.0.3)lib / active_record / query_cache.rb:31:in
activerecord (3.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:353:in
actionpack(3.0.3)lib / action_dispatch / middleware / callbacks.rb:46:in
activesupport (3.0.3) lib/active_support/callbacks.rb:415:in
actionpack(3.0.3)lib / action_dispatch / middleware / callbacks.rb:44:in
rack (1.2.1) lib/rack/sendfile.rb:107:in
actionpack(3.0.3)lib / action_dispatch / middleware / remote_ip.rb:48:in
actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:in
railties(3.0.3)lib / rails / rack / logger.rb:13:in
rack (1.2.1) lib/rack/runtime.rb:17:in
activesupport(3.0.3)lib / active_support / cache / strategy / local_cache.rb:72:in
rack (1.2.1) lib/rack/lock.rb:11:in
:10:在
rack (1.2.1) lib/rack/lock.rb:11:in
actionpack(3.0.3)lib / action_dispatch / middleware / static.rb:30:in
railties (3.0.3) lib/rails/application.rb:168:in
railties(3.0.3)lib / rails / application.rb:77:in
railties (3.0.3) lib/rails/rack/log_tailer.rb:14:in
rack(1.2.1)lib / rack / content_length.rb:13:in
rack (1.2.1) lib/rack/handler/webrick.rb:52:in
/Users/joshcrowder/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
/Users/joshcrowder/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in
/Users/joshcrowder/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in在start_thread中的块'
我认为您的问题在于您的关联命名。 由于你的
用户:
1 2 3 4 | class User < ActiveRecord::Base has_many :user_plans has_many :plans, :through => :user_plans accepts_nested_attributes_for :user_plans |
计划:
1 2 3 | class Plan < ActiveRecord::Base has_many :user_plans has_many :users, :through => :user_plans |
视图:
1 2 3 4 | <%= form_for(@user) do |f| %> <%= hidden_field_tag"user[user_plans][plan_id]", 2 %> <%= f.text_field :first_name %> </p> |
关于
希望这可以帮助!