1

我想使用单个 Active Admin 表单创建以下两个模型类:

class User < ActiveRecord::Base
    has_one :profile
    attr_accessible :email, :profile, :profile_attributes
    accepts_nested_attributes_for :profile
end

class Profile < ActiveRecord::Base
    belongs_to :user
    attr_accessible :name, :user_id
end

以下是我如何使用 Active Admin 将两个模型嵌套在一个“创建用户”表单中:

form do |f|
      f.inputs "User Fundamentals" do
        f.input :email
      end

      #f.inputs "User Profile", :for => :profile do |profile_form|
      f.inputs :name => "User Profile", :for => [f.object.profile || Profile.new] do |profile_form|
        profile_form.input :name
      end
      f.buttons
end

表格显示得非常好。但是单击“创建”后,出现以下错误:

ActiveRecord::AssociationTypeMismatch in Admin::UsersController#create

Profile(#70204018617440) expected, got ActiveSupport::HashWithIndifferentAccess(#70204014589500)

这是请求参数的样子:

{"utf8"=>"✓",
 "authenticity_token"=>"...",
 "user"=>{"email"=>"user@test.com",
 "profile"=>{"name"=>"my name"}},
 "commit"=>"Create User"}

这是跟踪:

activerecord (3.2.9) lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
activerecord (3.2.9) lib/active_record/associations/has_one_association.rb:8:in `replace'
activerecord (3.2.9) lib/active_record/associations/singular_association.rb:17:in `writer'
activerecord (3.2.9) lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
activerecord (3.2.9) lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'
activerecord (3.2.9) lib/active_record/attribute_assignment.rb:93:in `each'
activerecord (3.2.9) lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
activerecord (3.2.9) lib/active_record/base.rb:497:in `initialize'
inherited_resources (1.3.1) lib/inherited_resources/base_helpers.rb:52:in `new'
inherited_resources (1.3.1) lib/inherited_resources/base_helpers.rb:52:in `build_resource'
activeadmin (0.5.1) lib/active_admin/resource_controller/callbacks.rb:15:in `build_resource'
inherited_resources (1.3.1) lib/inherited_resources/actions.rb:31:in `create'
activeadmin (0.5.1) lib/active_admin/resource_controller/actions.rb:48:in `create'
actionpack (3.2.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.9) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.9) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.9) lib/active_support/callbacks.rb:447:in `_run__1741973691692741514__process_action__12141427009768339__callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.9) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.9) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.9) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.9) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.9) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.9) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:601:in `call'
omniauth (1.1.1) lib/omniauth/strategy.rb:177:in `call!'
omniauth (1.1.1) lib/omniauth/strategy.rb:157:in `call'
sass (3.2.3) lib/sass/plugin/rack.rb:54:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.9) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `_run__1872540156051940127__call__3542490292744583204__callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.9) lib/rails/engine.rb:479:in `call'
railties (3.2.9) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'

我试图解决这个问题很长一段时间(添加了accepts_nested_attributes_for等),但似乎无法找出可能导致这种情况的原因。因此,任何帮助将不胜感激!谢谢!

更新:我的路线:

batch_action_admin_users POST   /admin/users/batch_action(.:format)              admin/users#batch_action
                          admin_users GET    /admin/users(.:format)                           admin/users#index
                                      POST   /admin/users(.:format)                           admin/users#create
                       new_admin_user GET    /admin/users/new(.:format)                       admin/users#new
                      edit_admin_user GET    /admin/users/:id/edit(.:format)                  admin/users#edit
                           admin_user GET    /admin/users/:id(.:format)                       admin/users#show
                                      PUT    /admin/users/:id(.:format)                       admin/users#update
                                      DELETE /admin/users/:id(.:format)                       admin/users#destroy

更新 2:这是我的 routes.rb 文件:

MyApp::Application.routes.draw do
    resources :users
    resources :profiles
end
4

2 回答 2

1

终于设法解决了这个烦人的问题。这就是我的做法。

在迁移中,我将 user_id 的 t.integer 更改为 t.references 用户:

class CreateProfiles < ActiveRecord::Migration
  def change
     create_table :profiles do |t|
        t.references :user
        t.string :name
        t.timestamps
     end
  end
end

最重要的是,我在我的用户和个人资料模型中添加了以下几行:

class User < ActiveRecord::Base
  has_one :profile
  accepts_nested_attributes_for :profile
  attr_accessible :email, :profile
  alias_method :subtest=, :subtest_attributes= # <-- THIS is the new addition
end

现在它就像一个魅力:-)

于 2013-01-28T15:17:56.367 回答
0

好的,我认为这可能有效,

 resources :users do
 resources :profiles
 end
于 2012-12-20T18:31:57.590 回答