我正在尝试绘制一个折线图,该图表按照创建日期的顺序显示,我遇到了问题。
以下是导致错误的家庭控制器的index.html.erb代码段
<%= line_chart @orders.map { |order|
{name: order.id, data: order.created_at.group_by_day(:created_at, default_value: "missing", time_zone: "Kolkata").count}
} %>
我收到以下错误:
undefined method `group_by_day' for "2018-04-12T22:01:52-04:00":String
我的Gemfile中包含了groupdate和chartkick gems,这似乎是常见的修复。任何帮助,将不胜感激。如果您需要更多信息,请与我们联系。
完整追踪:
app/views/home/index.html.erb:14:in `block in _app_views_home_index_html_erb__895458771_51649740'
activeresource (5.0.0) lib/active_resource/collection.rb:8:in `map'
activeresource (5.0.0) lib/active_resource/collection.rb:8:in `map'
app/views/home/index.html.erb:13:in `_app_views_home_index_html_erb__895458771_51649740'
actionview (5.1.6) lib/action_view/template.rb:157:in `block in render'
activesupport (5.1.6) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.1.6) lib/action_view/template.rb:352:in `instrument_render_template'
actionview (5.1.6) lib/action_view/template.rb:155:in `render'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:52:in `block (2 levels) in render_template'
actionview (5.1.6) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.1.6) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:51:in `block in render_template'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:59:in `render_with_layout'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:50:in `render_template'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (5.1.6) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (5.1.6) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (5.1.6) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.1.6) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (5.1.6) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.1.6) lib/action_controller/metal/renderers.rb:141:in `render_to_body'
actionpack (5.1.6) lib/abstract_controller/rendering.rb:24:in `render'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (5.1.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/Ruby24-x64/lib/ruby/2.4.0/benchmark.rb:308:in `realtime'
activesupport (5.1.6) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:29:in `cleanup_view_runtime'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (5.1.6) lib/action_controller/metal/implicit_render.rb:33:in `default_render'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:108:in `block in run_callbacks'
shopify_app (8.2.6) lib/shopify_app/controller_concerns/login_protection.rb:15:in `shopify_session'
activesupport (5.1.6) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
omniauth (1.8.1) lib/omniauth/strategy.rb:190:in `call!'
omniauth (1.8.1) lib/omniauth/strategy.rb:168:in `call'
omniauth (1.8.1) lib/omniauth/builder.rb:63:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.6.0) lib/web_console/middleware.rb:137:in `call_app'
web-console (3.6.0) lib/web_console/middleware.rb:22:in `block in call'
web-console (3.6.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.6.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.6) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
打印时的示例顺序:
#<ShopifyAPI::Order:0x00000000086f6780 @attributes={"id"=>380632137775, "email"=>"", "closed_at"=>nil, "created_at"=>"2018-04-12T22:01:52-04:00", "updated_at"=>"2018-04-12T22:01:52-04:00", "number"=>2, "note"=>"", "token"=>"hidden", "gateway"=>nil, "test"=>false, "total_price"=>"0.00", "subtotal_price"=>"0.00", "total_weight"=>0, "total_tax"=>"0.00", "taxes_included"=>false, "currency"=>"USD", "financial_status"=>"paid", "confirmed"=>true, "total_discounts"=>"0.00", "total_line_items_price"=>"0.00", "cart_token"=>nil, "buyer_accepts_marketing"=>false, "name"=>"#1002", "referring_site"=>nil, "landing_site"=>nil, "cancelled_at"=>nil, "cancel_reason"=>nil, "total_price_usd"=>"0.00", "checkout_token"=>nil, "reference"=>nil, "user_id"=>10752655407, "location_id"=>6268256303, "source_identifier"=>nil, "source_url"=>nil, "processed_at"=>"2018-04-12T22:01:52-04:00", "device_id"=>nil, "phone"=>nil, "customer_locale"=>nil, "app_id"=>1354745, "browser_ip"=>nil, "landing_site_ref"=>nil, "order_number"=>1002, "discount_codes"=>[], "note_attributes"=>[], "payment_gateway_names"=>[], "processing_method"=>"manual", "checkout_id"=>nil, "source_name"=>"shopify_draft_order", "fulfillment_status"=>nil, "tax_lines"=>[], "tags"=>"", "contact_email"=>nil, "order_status_url"=>"hidden", "line_items"=>[#<ShopifyAPI::LineItem:0x000000000866fc58 @attributes={"id"=>783789817903, "variant_id"=>7469149978671, "title"=>"Product #1", "quantity"=>1, "price"=>"0.00", "sku"=>"", "variant_title"=>nil, "vendor"=>"new-vision-nutritio-dev", "fulfillment_service"=>"manual", "product_id"=>639214125103, "requires_shipping"=>true, "taxable"=>true, "gift_card"=>false, "name"=>"Product #1", "variant_inventory_management"=>nil, "properties"=>[], "product_exists"=>true, "fulfillable_quantity"=>1, "grams"=>0, "total_discount"=>"0.00", "fulfillment_status"=>nil, "tax_lines"=>[#<ShopifyAPI::TaxLine:0x000000000865dcb0 @attributes={"title"=>"NJ State Tax", "price"=>"0.00", "rate"=>0.06625}, @prefix_options={}, @persisted=true>]}, @prefix_options={}, @persisted=true>], "shipping_lines"=>[], "fulfillments"=>[], "refunds"=>[]}, @prefix_options={}, @persisted=true>
答案 0 :(得分:2)
如果查看groupdate
的文档,
group_by_day
方法适用于对象集合,而不是特定属性。
以下代码适用于订单数组:
<%=
line_chart @orders.group_by_day(default_value: "missing", time_zone: "Kolkata") { |order|
order.created_at
}.map { |date, orders| [date, orders.size] }
%>
在groupdate
上的文档中,group_by_day
是一个处理对象集合的方法,可以是Enumerable或ActiveRecord集合。在类似数组的Enumerable
对象上调用时,它使用块的返回值来知道要分组的对象的哪个属性,在本例中为created_at
属性。对map
的最后一个方法调用返回chartkick
可用于呈现折线图的数据,在这种情况下,chartkick需要一个数组数组,其中包含每个日期的日期和项目数(或者某些聚合项)值)。