I want to be able to store my xml into a temporary file and then send it to another method in another controller to be processed. Currently it will not allow me to read the file, once sent, because it is a private method.
Controller #1
xml_doc = Nokogiri::XML(@builder.to_xml)
@tempfile = Tempfile.new('xml')
@tempfile.write(xml_doc)
redirect_to upload_xml_admin_events_path(:file => @tempfile)
Controller #2
Version #1
xml = params[:file].read
xmldoc = Nokogiri::XML(xml)
Gives me this error: undefined method `read' for "File:0x6ebfb00":String
Version #2
xml = params[:file]
xml.open
xmldoc = Nokogiri::XML(xml)
Gives me this error: private method `open' called for "#File:0x6a12bd8":String
It seems like you are thinking that params can be objects, which can be forgiven due to Rails magic. In reality all params are strings with a key in the key=value format.
So the issue here is that when you redirect with the parameter 'file', it turns your Tempfile object into a string. This is why the error is telling you that there are no accessible methods called read or open for a string. I see a few options:
Do whatever you are going to do with the file on Controller 1 instead of redirecting to Controller 2. You won't have to create extra objects, hit a database, or have crazy parameters in your URL.
If the XML can be really large, it might be better to make an AR object called XmlFile or something and write it to the database in Controller 1, then redirect with that id in the parameters. This way, you won't have to send crazy long XML strings in the URL (which is bad):
# Controller 1
@xml = XmlFile.new(@builder.to_xml)
redirect_to upload_xml_admin_events_path(:xml => @xml) #implicitly @xml.to_s
# Controller 2
@xml = XmlFile.find(params[:xml])
Nokogiri::XML(@xml)
If the XML is always (very) small, you can send the XML as a param in plain text (this seems to be closest to what you are currently doing, but strikes me as less elegant) You might run into URL encoding issues here though.
# Controller 1
xml = @builder.to_xml
redirect_to upload_xml_admin_events_path(:xml => xml)
# Controller 2
@xml = Nokogiri::XML(params[:xml])