将局部变量发送到ActiveAdmin的部分内部是不行的

时间:2015-09-01 18:01:06

标签: ruby-on-rails activeadmin

我有一个ActiveAdmin页面如下:

ActiveAdmin.register_page "Pretty Demo Day" do
  menu label: "Bi-weekly", parent: "Demo Day Statistics"

  this_week = [14.days.ago.to_date..Date.today, "this_week"]
  two_weeks_ago = [28.days.ago.to_date...14.days.ago.to_date, "two_weeks_ago"]
  all_time = [Date.parse("2014-01-01")..Date.today, "all_time"]

  @periods = [this_week, two_weeks_ago, all_time]

  content title: I18n.t("active_admin.demo_day_stats.title") do
    render partial: "admin/shared/demo_day_metrics", locals: { periods: @periods }
  end
end

部分看起来像这样:

%table
  %thead
    %th "Inside the partial"
    - periods.each do |p|
      %th
        - format_period(p)

  %tbody
    - metrics.each do |name, *values|
      %tr
        %td name

        - values.each do |v|
          %td
            - case v
            - when Numeric
              number_with_delimiter(v)
            - when String
              v
            - else
              "N/A"

当我尝试加载页面时,我收到错误:第4行undefined method each for nil:NilClass,包含periods.each...

的错误

我已将binding.pry插入partial并查看了局部变量。变量periods在那里,但是为零。我已经验证在register_page文件中,@periods具有正确的值。

如何调用部分并发送一组时间段? ActiveAdmin是否对阻止数据传递给部分的部分内容做了一些特殊的事情?

1 个答案:

答案 0 :(得分:2)

您无法在您使用的地方实例化变量。必须在content块中实例化该变量。

ActiveAdmin.register_page "Pretty Demo Day" do
  menu label: "Bi-weekly", parent: "Demo Day Statistics"

  content title: I18n.t("active_admin.demo_day_stats.title") do
    this_week = [14.days.ago.to_date..Date.today, "this_week"]
    two_weeks_ago = [28.days.ago.to_date...14.days.ago.to_date, "two_weeks_ago"]
    all_time = [Date.parse("2014-01-01")..Date.today, "all_time"]

    @periods = [this_week, two_weeks_ago, all_time]

    render partial: "admin/shared/demo_day_metrics", locals: { periods: @periods }
  end
end