将局部变量传递给 Rails 中的 :file render 方法

发布于 2024-11-27 22:19:37 字数 611 浏览 0 评论 0原文

我在将变量传递给构建器文件以生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-12-04 22:19:37

正如 @Devin 评论的那样,“x”应该是“@x”。我还建议不要在视图中设置变量,尝试使用变量来显示控制器相应方法中的数据集(并尽可能将更改数据的任何操作作为模型的一部分)。

# app/controllers/my_controller.rb

class MyController < ApplicationController
  def index
    @x = [Date.new(2010, 8, 5).to_time.to_i, DateTime.new(2011, 8, 5).to_time.to_i]
  end
end

# app/views/my/index.erb

<%str_xml = render :file=>"filepath/file", :locals=>{: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).

# app/controllers/my_controller.rb

class MyController < ApplicationController
  def index
    @x = [Date.new(2010, 8, 5).to_time.to_i, DateTime.new(2011, 8, 5).to_time.to_i]
  end
end

# app/views/my/index.erb

<%str_xml = render :file=>"filepath/file", :locals=>{:x => @x} %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文