Evaluate code as soon as the presentation in rails is visualized

In rails, I created a kind of cronjob, but as soon as the view is visualized, there is code that I would like to run as a "signoff" for the task to be processed.

Where would I put the code so that it runs at the absolute end of processing (after rendering the view)

How does the html rails process handle? (Will he clear the HTML from the user as his rendering, or after his rendering?)

+4
source share
2 answers

You can visualize the presentation in a line, catch any timeouts that may occur during rendering, register the results of the action and return the line:

begin @elements = Element.find(:all) html = render_to_string # Store the result Result.create(:element_count => @elements.count) rescue Timeout::Error # Store the result of the call as failed? Result.create(:element_count => 0) end send_data html, :disposition => 'inline', :type => 'text/html' 

Some other things you can do to achieve your goal may be:

  • You can use the rake task instead of the controller action if the code you need to execute is run only by your cron task.

  • Instead of directly asking for the use of wget in your cron job, you can call a script that will make a request to your controller, check the output of the request, and then register the result (possibly calling a new action in the controller).

+1
source

To select an element from the displayed HTML, you can read the value using javascript / jQuery:

  $(document).ready(function() { var completion = $('#items_processed').val(); }); 
0
source

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


All Articles