Loading a page into memory in Rails

My rails application creates XML when loading / reports / generate _report.

On a separate page, I want to read this XML in a variable and store it in a database.

How can i do this? Is there any way to pass the response from / reports / generate _report.xml URI to a variable? Or is there a better way to do this since XML is being created by the same web application?

Here is my generate_report action:

class ReportsController < ApplicationController def generate_report respond_to do |format| @products = Product.all format.xml { render :layout => false } end end end 

Here is the action I'm trying to write:

 class AnotherController < ApplicationController def archive_current @output = # get XML output produced by /reports/generate_report # save @output to the database respond_to do |format| format.html # inform the user of success or failure end end end 

Solved: My solution (thanks to Mladen Yablanovich):

 @output = render_to_string(:file => 'reports/generate_report.xml.builder') 

I used the following code in the model class to accomplish the same task, since render_to_string is the (idiotic) protected ActionController :: Base method:

 av = ActionView::Base.new(Rails::Configuration.new.view_path) @output = av.render(:file => "reports/generate_report.xml.builder") 
+4
source share
4 answers

Perhaps you could extract your XML visualization for a separate method within the same controller (possibly private), which would turn XML into a string using render_to_string and name it from both generate_report and archive_current actions.

+2
source

What I usually do in this type of situation is to create a separate module / class / model for creating a report (this may even be true in the product model). This separate component can be in app/models or it can be in lib . In any case, once you remove it, you can use it anywhere. The controller can call it directly. You can create it from the console. You can get the cron job. This is not only more flexible, but also helps smooth out the response time of a request if the report becomes slow to generate.

Since you are using a template, it’s clear that the controller’s route is convenient, but even if you need to include some ruby ​​template system in your auxiliary library, it will probably be less complex and more flexible than trying to go through the controller.

+1
source

@output = Product.all.to_xml

+1
source

Sorry, are you in doubt about Xml or about sessions? I mean, what is your action generating the Xml stuff to the question? Or do you just want to save the result of the action for the last use?
You said on a "separate" page - do you mean by another request? (for example, after the user has approved it?) Why do you want to save the result? Because it has to be saved exactly as visualized? (for example, a user may be disappointed if he clicked to save one report and you saved another) Or is this a thing that needs to be generated?
Or maybe I realized that this is wrong, and about refactoring?

0
source

Source: https://habr.com/ru/post/1305911/


All Articles