将局部变量传递给 Rails 中的 :file render 方法
我在将变量传递给构建器文件以生成 xml 时遇到问题,并且认为我可能完全错误地解决了该问题,但这是我当前的尝试:
<% @x = [1000, 2000, 3000] %>
<%str_xml = render :file=>"filepath/file", :locals=>{:x => x} %>
然后在 file.builder 内部我尝试访问 x,但是继续获取
undefined local variable or method `x'
我尝试在构建器中访问它的位置,因为
xml.dataset(:seriesName => 'Bogus') do
for element in x
xml.set(:value=>element)
end
end
只要我不尝试访问 x 变量,代码就可以正常工作。最终 x 将变成两个日期,将转换为时间戳,并传递给 xml 构建器以在数据库查询中使用,以确定要显示的数据的时间范围。我对 Rails 很陌生,所以我完全接受我正在倒退的想法。感谢任何和所有的帮助!
谢谢
I'm having an issue passing a variable to a builder file for xml generation, and think I might be approaching the problem completely wrong, but here is my current attempt:
<% @x = [1000, 2000, 3000] %>
<%str_xml = render :file=>"filepath/file", :locals=>{:x => x} %>
And then inside of the file.builder I try to access x, but keep getting
undefined local variable or method `x'
Where I try to access it in the builder as
xml.dataset(:seriesName => 'Bogus') do
for element in x
xml.set(:value=>element)
end
end
The code works fine as long as I don't try to access the x variable. Eventually the x will become two dates that will be converted to timestamps, and passed to the xml builder to be used in the database queries to determine to the time range of data to display. I'm very green with rails, so am completely open to the idea that I'm approaching the idea backwards. Any and all help are appreciated!
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @Devin 评论的那样,“x”应该是“@x”。我还建议不要在视图中设置变量,尝试使用变量来显示控制器相应方法中的数据集(并尽可能将更改数据的任何操作作为模型的一部分)。
As @Devin commented 'x' should be '@x'. I'd also recommend not setting variables in the view, try to have variables used to display data set in the corresponding method of the controller (and as much as possible have any actions that change data be part of the model).