我正在使用Rails 4和Mongoid。我有以下型号:
class Country
include Mongoid::Document
field :iso
field :name
embedded_in :user
end
class User
include Mongoid::Document
field :email
field :name
embeds_one :country
end
在编辑用户视图时,我想将下拉列表放在国家/地区,以便选择所选国家/地区作为嵌入用户记录。我试着用这样的东西:
<%= f.select :country, Country.all.collect { |c| [c.name, c._id] }, :prompt => 'Please select' %>
但它看起来不起作用:
undefined method `__metadata' for "5324c9f7416c65a032070000":String
任何建议都会有所帮助。感谢。
Started POST "/users" for 127.0.0.1 at 2014-03-16 16:31:32 +0200
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UzETkbI3MNGBJQg7IhzI6jD5dQCZrvjU0JAgkReTSak=", "user"=>{"first_name"=>"", "last_name"=>"", "email"=>"", "birth_date"=>"", "gender"=>"", "marital_status"=>"", "city"=>"", "country"=>"5324c9f5416c65a032000000", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `__metadata' for "5324c9f5416c65a032000000":String:
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/proxy.rb:141:in `characterize_one'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/embedded/one.rb:20:in `block in initialize'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/proxy.rb:42:in `init'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/embedded/one.rb:19:in `initialize'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/accessors.rb:44:in `new'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/accessors.rb:44:in `create_relation'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/accessors.rb:26:in `__build__'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/accessors.rb:233:in `block (2 levels) in setter'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/accessors.rb:140:in `without_autobuild'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/relations/accessors.rb:229:in `block in setter'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/attributes/processing.rb:141:in `block in process_relations'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/attributes/processing.rb:136:in `each_pair'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/attributes/processing.rb:136:in `process_relations'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/attributes/processing.rb:122:in `process_pending'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/attributes/processing.rb:29:in `process_attributes'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/document.rb:110:in `block in initialize'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
() Users/alex/.rvm/gems/ruby-2.1.0/bundler/gems/mongoid-164755a9d038/lib/mongoid/document.rb:104:in `initialize'
devise (3.2.3) lib/devise/models/registerable.rb:20:in `new'
devise (3.2.3) lib/devise/models/registerable.rb:20:in `new_with_session'
devise (3.2.3) app/controllers/devise/registrations_controller.rb:95:in `build_resource'
devise (3.2.3) app/controllers/devise/registrations_controller.rb:13:in `create'
actionpack (4.0.4) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.4) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.4) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.4) lib/active_support/callbacks.rb:433:in `_run__4022758027926609887__process_action__callbacks'
activesupport (4.0.4) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.4) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.4) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.4) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
actionpack (4.0.4) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.4) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.4) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.4) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.4) lib/action_dispatch/routing/mapper.rb:44:in `call'
actionpack (4.0.4) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in `call'
omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/cookies.rb:486:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.4) lib/active_support/callbacks.rb:373:in `_run__3804891227325973598__call__callbacks'
activesupport (4.0.4) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.0.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.4) lib/rails/engine.rb:511:in `call'
railties (4.0.4) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/alex/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/alex/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/alex/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
答案 0 :(得分:0)
问题是您没有在用户中正确设置国家/地区...
尝试将以下内容添加到用户模型中 如果你想以正常的mongoid方式设置国家或用id ..
def country
@country ||= Country.where(id: country_id).first
end
def country=(obj)
case obj.class
when String
self.country_id = obj
@country = Country.find(obj)
when Country
self.country_id = obj.id
@country = obj
end
end