我应该如何从模块返回哈希值?

时间:2018-10-12 12:48:47

标签: ruby-on-rails ruby

请原谅我的英语不完美。

我有一个控制器,可以在其中设置几张图表的数据。在这种方法中,我有一个块,可以在几种方法中使用,因此我创建了一个模块来放置此代码,并将其用于需要的方法中(应该这样做还是在控制器中创建一个方法?)。

问题是我的模块中的哈希返回为零(但我的模块中没有)。

你能告诉我我的错误在哪里吗? 我在if params[:checked] == 'true'

之后从模块中调用该方法

控制器:

module Admin
  class AdminController < ApplicationController
    include WeeklyCharts #=> I include my module

    def signup_stats(start_date, end_date)

      customer_signup_chart = {
        type: 'line',
        data: [],
        label: 'New customer registrations'
      }

      freelance_signup_chart = {
        type: 'line',
        data: [],
        label: 'New freelance registrations'
      }

      users = User
        .where('created_at > :start_date AND created_at < :end_date',
          start_date: start_date.beginning_of_day,
          end_date: end_date.end_of_day)
        .select(:created_at, :typology)

      if params[:checked] == 'true'
        set_datasets_labels(start_date, end_date) #=> Method in my module
        users = users.group_by_week(week_start: start_date.strftime('%a').downcase.to_sym, format: '%j %y') { |u| u.created_at }

        loop do
          client_signup_chart[:data] << (users[start_date.to_date.strftime('%j %y')] ? users[start_date.to_date.strftime('%j %y')].count {|u| u.typology == 'customer'} : 0)
          presta_signup_chart[:data] << (users[start_date.to_date.strftime('%j %y')] ? users[start_date.to_date.strftime('%j %y')].count {|u| u.typology == 'freelance'} : 0)
          start_date += 1.week
          break if start_date > end_date
        end

      else
        datasets = { labels: (start_date.to_datetime..end_date.to_datetime).map {|date| I18n.l(date, format: :date)}}
        users =  users.group_by { |u| u.created_at.to_date }

        loop do
          client_signup_chart[:data] << (users[start_date.to_date] ? users[start_date.to_date].count {|u| u.typology == 'customer'} : 0)
          presta_signup_chart[:data] << (users[start_date.to_date] ? users[start_date.to_date].count {|u| u.typology == 'freelance'} : 0)
          start_date += 1.day
          break if start_date > end_date
        end
      end

      datasets[:charts] = [customer_signup_chart, freelance_signup_chart] #=> undefined method `[]=' for nil:NilClass
      datasets
    end
  end
end

我的模块:

module WeeklyCharts

    def set_datasets_labels(start_date, end_date)
        start_day = start_date.strftime('%A').downcase.to_sym
        datasets = { labels: (start_date.to_datetime..end_date.to_datetime).map {|date| date.beginning_of_week(start_day = start_day).strftime("From %d/%m to #{date.end_of_week(start_day = start_day).strftime('%d/%m')}")}.uniq}
        datasets[:labels][0] = "From #{start_date.strftime('%d/%m')} to #{start_date.end_of_week(start_day = start_day).strftime('%d/%m')}"
        datasets[:labels][-1] = "From #{end_date.beginning_of_week(start_day = start_day).strftime('%d/%m')} to #{end_date.strftime('%d/%m')}"

        datasets
    end

end

错误返回:

datasets[:charts] = [customer_signup_chart, freelance_signup_chart]
#=> undefined method `[]=' for nil:NilClass

此外,我可以从模块中p datasets,它只是在我的控制器中nil

如果您需要更多详细信息,请问我。

0 个答案:

没有答案