Passing a local variable to :file render method in 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 开发者_如何学Cdates 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
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} %>
精彩评论