将局部变量发送到 ActiveAdmin 的部分内部不起作用
Sending local variables into a partial inside of ActiveAdmin isn't working
我有一个 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 并查看了局部变量。变量 periods
在那里,但为零。我已验证在 register_page 文件中,@periods 具有正确的值。
如何调用部分时间段并发送? ActiveAdmin 是否对部分进行了特殊处理以防止数据被传递到部分?
你不能在你使用的那个地方实例化一个变量。变量必须在 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
我有一个 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 并查看了局部变量。变量 periods
在那里,但为零。我已验证在 register_page 文件中,@periods 具有正确的值。
如何调用部分时间段并发送? ActiveAdmin 是否对部分进行了特殊处理以防止数据被传递到部分?
你不能在你使用的那个地方实例化一个变量。变量必须在 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