在传递给 def 的数组中使用变量 - Rails
我正在使用 Google 图表 API 在 Rails 应用程序中生成饼图。但是,我在将局部变量传递给助手中的 def 时遇到问题。 def 采用 [label, value] 对的二维数组。当我尝试将局部变量作为值传递时,它不喜欢它。这些是提前计算的并采用货币格式。将变量放在引号或 #{}
中也不起作用。
application_helper.rb
def pie_chart(data, options = {})
options[:width] ||= 250
options[:height] ||= 100
options[:colors] = %w(F5DD7E 0DB2AC FC8D4D FC694D FABA32 704948 968144 C08FBC ADD97E)
dt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."
options[:divisor] ||= 1
options[:title] ||= "Energy Costs"
while (data.map { |k,v| v }.max / options[:divisor] >= 4096) do
options[:divisor] *= 20
end
opts = {
:cht => "p",
:chd => "e:#{data.map{|k,v|v=v/options[:divisor];dt[v/64..v/64]+dt[v%64..v%64]}}",
:chl => "#{data.map { |k,v| CGI::escape(k)}.join('|')}",
:chs => "#{options[:width]}x#{options[:height]}",
:chco => options[:colors].slice(0, data.length).join(','),
:chf => "bg,s,FFFFFF00",
:chtt => "#{options[:title]}"
}
视图中的 def 调用:
<%= pie_chart( [["Light Cost", #{light_cost}], ["Small Appliance Cost", #{sm_appl_cost}]], :width => 550, :height => 200) %>
如何传递局部变量?
I'm using the Google charts API to generate a pie chart in my Rails application. However, I'm having a problem passing local variables to the def in the helper. The def takes a 2D array of [label, value] pairs. It doesn't like it when I try to pass a local variable in as the value. These are calculated ahead of time and are in currency format. Putting the variable in quotes or in #{}
doesn't work either.
application_helper.rb
def pie_chart(data, options = {})
options[:width] ||= 250
options[:height] ||= 100
options[:colors] = %w(F5DD7E 0DB2AC FC8D4D FC694D FABA32 704948 968144 C08FBC ADD97E)
dt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."
options[:divisor] ||= 1
options[:title] ||= "Energy Costs"
while (data.map { |k,v| v }.max / options[:divisor] >= 4096) do
options[:divisor] *= 20
end
opts = {
:cht => "p",
:chd => "e:#{data.map{|k,v|v=v/options[:divisor];dt[v/64..v/64]+dt[v%64..v%64]}}",
:chl => "#{data.map { |k,v| CGI::escape(k)}.join('|')}",
:chs => "#{options[:width]}x#{options[:height]}",
:chco => options[:colors].slice(0, data.length).join(','),
:chf => "bg,s,FFFFFF00",
:chtt => "#{options[:title]}"
}
The def call in the view:
<%= pie_chart( [["Light Cost", #{light_cost}], ["Small Appliance Cost", #{sm_appl_cost}]], :width => 550, :height => 200) %>
How do I pass the local variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你得到的错误是什么?您应该能够这样调用它:
我在哈希周围添加了
{}
只是为了使其更加清晰和明确。使用
#{var}
不是此处的方法,因为这是在字符串中进行替换,例如“Here is the value: #{var}”
。What's the error you get? You should be able to call it like:
I added
{}
's around the hash just to make it clearer and explicit.Using
#{var}
is not the way to do it here because that's for substitution in a string, eg"Here is the value: #{var}"
.因此,该代码肯定需要数值,但
#{}
只是开始注释,除非在字符串内使用,并且您不希望在那里有字符串。实际上,如果它的唯一用途是与 2 变量
一起使用,那么所写的 pie_chart 似乎会采用您所说的 2D 数组或 哈希。映射迭代器。因此,您应该能够使用
[["str1", light_cost], ["str2", sm_appl_cost]]
或{ 'str1'=>light_cost, 'str2'=> ;sm_appl_cost}
。不过,您确实需要确保这些当地人是数字。如果不是,请尝试对它们使用
.to_i
或.to_f
。So that code definitely wants numeric values, but
#{}
is just going to start a comment unless used inside a string, and you don't want a string there.And actually, pie_chart as written would appear to take either a 2D array as you say OR a Hash, if the only use of it is with 2-variable
.map
iterators. So you should be able to use either[["str1", light_cost], ["str2", sm_appl_cost]]
or{ 'str1'=>light_cost, 'str2'=>sm_appl_cost}
.You do need to make sure that those locals are numeric, though. Try using
.to_i
or.to_f
on them if they aren't.