1

我有以下兩個模型類,我想創建一個單一的活動聯繫方式:Rails的主動聯繫表 - 嵌套模式

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 

下面是我窩兩個模型在一個單一的「創建用戶「使用活動管理的形式:

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"=>"[email protected]", 
"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' 

我試着圖這一個出了很長一段時間(添加accep_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 
+0

你可以顯示你的路線嗎?可能是資源用戶做資源配置文件 – Richlewis

+0

謝謝,更新我的帖子與相關路線使用耙路線。 –

+0

對不起,我的意思是配置/路線 – Richlewis

回答

1

最後管理解決這個煩人的問題。這是我做到的。

在遷移我改變了對t.integer USER_ID到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 

現在它像一個魅力工作:-)

0

好,我想這可能工作,

resources :users do 
resources :profiles 
end 
+0

你也有配置文件模型中的外鍵嗎?所以用戶模型 – Richlewis

+0

中的profile_id對我遲到的答案感到抱歉!新年快樂btw。好的,我遵循了你的建議,並添加了:按照你的方式描述。我也更新了我的用戶模型的方式如下: attr_accessible:輪廓,:profile_attributes,:PROFILE_ID 不幸的是,我仍然得到同樣的錯誤信息:配置文件(#41461164)預計,得到的ActiveSupport :: HashWithIndifferentAccess(#23680572) 任何其他想法可能會導致這個問題? –

+0

是否有可能(自己創建的)配置文件哈希不代表由「資源配置文件」定義的完整且正確的配置文件哈希?如果是這樣,我應該添加像「ID」和某些日期(創建日期等)的參數? –