1

I have the following two model classes I'd like to create with a single Active Admin form:

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

Here's how I nest the two models in a single "Create User" form using 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

The form is being displayed perfectly fine. But after clicking "Create" I get the following error:

ActiveRecord::AssociationTypeMismatch in Admin::UsersController#create

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

This is how the request parameter looks like:

{"utf8"=>"✓",
 "authenticity_token"=>"...",
 "user"=>{"email"=>"[email protected]",
 "profile"=>{"name"=>"my name"}},
 "commit"=>"Create User"}

Here's the trace:

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'

I tried to figure this one out for quite a while (added accepts_nested_attributes_for, etc.) but can't seem to find out what might cause this. Therefore any help would be greatly appreciated! Thanks!

Update: my routes:

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

Update 2: Here's my routes.rb file:

MyApp::Application.routes.draw do
    resources :users
    resources :profiles
end
4
  • can you show your routes please? could be resources user do resources profile Commented Dec 20, 2012 at 15:19
  • Thanks, updated my post with the relevant routes using rake routes. Commented Dec 20, 2012 at 15:36
  • sorry i meant config/routes Commented Dec 20, 2012 at 15:45
  • Alright, here you go. What do you mean by "resources user do resources profile"? Commented Dec 20, 2012 at 17:45

2 Answers 2

1

Finally managed to solve this annoying problem. Here's how I did it.

In migration I changed the t.integer for user_id to t.references user:

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

On top of that I added the follow lines to my users & profiles model:

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

Now it's working like a charm :-)

Sign up to request clarification or add additional context in comments.

Comments

0

ok i think this may work,

 resources :users do
 resources :profiles
 end

8 Comments

also do you have a foreign key in the profile model? so profile_id in the user model
sorry for my late answer! Happy new year btw. Ok, I followed your advice and added :profiles the way you did. I also updated my user model the following way: attr_accessible :profile, :profile_attributes, :profile_id Unfortunately I am still getting the same error message: Profile(#41461164) expected, got ActiveSupport::HashWithIndifferentAccess(#23680572) Any other ideas what might cause this problem?
Is it possible that the (self created) profile hash does not represent the full and proper profile hash that is defined by "ressource profile"? If so, should I add parameters like "id" and certain dates (date of creation, etc.)?
hmm I'm thinking its your form looking at it. The form you have posted above, is that how your form looks now, can you post including all <%= etc
I thought the actual forms are being generated by active_admin during runtime. So far I never had to create a custom form/partial as described here: activeadmin.info/docs/5-forms.html There are no forms und app/views/admin available.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.