ruby-on-railschartkick

Chartkick: Undefined method `line_chart'


I am working on a Rails 6 application and I am trying setup Chartkick for charts in my application.

I installed Chartkick this way:

  1. For Rails 6 / Webpacker, run:

    yarn add chartkick chart.js
    
  2. And in app/javascript/packs/application.js, add:

    require("chartkick")
    require("chart.js")
    

3 Install Groupdate to use the group_by_day method

gem 'groupdate'

And then run bundle install.

However, when I added this line to my Dashboard views:

<%= line_chart @products.group_by_day(:created_at).count %>

To show a graph for products using the @products variable:

class Admins::DashboardController < ApplicationController
  before_action :authenticate_admin!

  # GET /dashboards
  # GET /dashboards.json
  def index
    @products = Product.all
  end
end

I get the following error:

undefined method `line_chart'

I'm wondering what I am doing wrong.


Solution

  • I finally figured it out.

    Here's how I fixed it:

    The issue was that I was missing the first requirement which is :

    Adding this line to your application's Gemfile:

    gem "chartkick"
    

    And then running bundle install

    My initial misconception was that I did not need this requirement since I am working with a Rails 6 application.

    I thought that this step only applies to Rails 5 applications and below and not on Rails 6 applications since I am running yarn add chartkick chart.js. But it turned out to be untrue. It is required for both Rails 6 applications and Rails 5 applications and below.

    That's all.

    I hope this helps