因此,我使用Rails 4.2.0的Ruby 2.1.3,並使用active_model_serializer gem將適當的json數據發送到AngularJs。獲取TypeError - 無法克隆符號:使用active_model_serializer時出錯
不僅我今天只從Rails 4.1.6升級到4.2.0。對於以前的版本,問題不存在。
當我嘗試從我的媒體節目的行動得到JSON出現問題:
def show
medium = Medium.find(params[:id])
respond_with medium, include: [{comments: :user}, :likes, :user]
end
這是怎麼了同樣的動作看之前我升級的軌道:
def show
medium = Medium.includes([{comments: :user}, :likes, :user]).find(params[:id])
respond_with medium
end
它曾經是能夠以舊的方式工作,但使用新的導軌時,它不會在發送的json數據中包含帶有媒體的ActiveModels。
所以我改變了使用此頁面上的示例代碼:https://github.com/rails-api/active_model_serializers
這裏是我的媒體串行看起來像:
class MediumSerializer < ActiveModel::Serializer
attributes :id,
:text,
:image_video_file_name,
:image_video_croppable_url,
:image_video_thumb_url,
:image_video_big_url
has_one :user
has_many :comments, :likes
end
升級之前,它是這樣的:
class MediumSerializer < ActiveModel::Serializer
attributes :id,
:text,
:image_video_file_name,
:image_video_croppable_url,
:image_video_thumb_url,
:image_video_big_url
has_one :user
has_many :comments, :likes
def include_user?
object.association(:user).loaded?
end
def include_comments?
object.association(:comments).loaded?
end
def include_associations?
object.association(:user, :comments, :likes).loaded?
end
end
因此,當對該show動作進行查詢時,我的Rails應用程序拋出的錯誤是:
Started GET "/media/7.json" for 127.0.0.1 at 2014-12-25 18:13:43 +0200
Processing by Content::MediaController#show as JSON
Parameters: {"id"=>"7"}
Medium Load (0.4ms) SELECT "media".* FROM "media" WHERE "media"."id" = $1 LIMIT 1 [["id", 7]]
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."medium_id" = $1 [["medium_id", 7]]
Completed 500 Internal Server Error in 59ms
TypeError - can't clone Symbol:
activesupport (4.2.0) lib/active_support/core_ext/object/try.rb:77:in `try!'
activesupport (4.2.0) lib/active_support/core_ext/object/try.rb:63:in `try'
activerecord (4.2.0) lib/active_record/serialization.rb:12:in `serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:114:in `block (2 levels) in serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:114:in `block in serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:158:in `block in serializable_add_includes'
activemodel (4.2.0) lib/active_model/serialization.rb:156:in `serializable_add_includes'
activemodel (4.2.0) lib/active_model/serialization.rb:112:in `serializable_hash'
activerecord (4.2.0) lib/active_record/serialization.rb:17:in `serializable_hash'
activemodel (4.2.0) lib/active_model/serializers/json.rb:99:in `as_json'
app/models/medium.rb:32:in `as_json'
activesupport (4.2.0) lib/active_support/json/encoding.rb:34:in `encode'
activesupport (4.2.0) lib/active_support/json/encoding.rb:21:in `encode'
activesupport (4.2.0) lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:116:in `block in <module:Renderers>'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:45:in `block in _render_to_body_with_renderer'
/home/kaspar/.rbenv/versions/2.1.3/lib/ruby/2.1.0/set.rb:263:in `each'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/kaspar/.rbenv/versions/2.1.3/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
responders (2.0.2) lib/action_controller/responder.rb:258:in `display'
responders (2.0.2) lib/action_controller/responder.rb:212:in `api_behavior'
responders (2.0.2) lib/action_controller/responder.rb:191:in `rescue in to_format'
responders (2.0.2) lib/action_controller/responder.rb:185:in `to_format'
responders (2.0.2) lib/action_controller/responder.rb:163:in `respond'
responders (2.0.2) lib/action_controller/responder.rb:156:in `call'
responders (2.0.2) lib/action_controller/respond_with.rb:203:in `respond_with'
app/controllers/content/media_controller.rb:17:in `show'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802: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 `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
passenger (4.0.56) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
passenger (4.0.56) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
passenger (4.0.56) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
passenger (4.0.56) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
值得注意的是,如果我刪除兩個:用戶包括從新的include:
方法,那麼一切正常。但是我希望每個評論也包括創建它的用戶和媒體也包括上傳它的用戶。
這裏有什麼問題?讓我知道是否需要更多信息。
編輯:問題似乎只與:user
爲comments:
。
發佈我的意見串行這裏:
class CommentSerializer < ActiveModel::Serializer
attributes :id,
:text,
:user_id,
:medium_id
has_one :user
end