Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After several hours and lots of other posts I got to a final solution:</p> <p>Bricker is right in that it is not possible for rails to render more than once in a call, as taken from <a href="http://guides.rubyonrails.org/layouts_and_rendering.html" rel="nofollow">http://guides.rubyonrails.org/layouts_and_rendering.html</a> "Can only render or redirect once per action"</p> <p>The site also states "The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller’s view path and render it."</p> <p>Then, the solution that worked great for me was to tell the controller to render to a string if a download flag (download=true) was set in :params (I also use request.url to have it working from any view in my application)</p> <p>View:</p> <pre><code>= link_to 'Download', request.url+"&amp;downloadexcel=true", :class =&gt; 'btn btn-primary btn-block' </code></pre> <p>Controller:</p> <pre><code>def acontrolleraction #some controller code here if params[:downloadexcel] save_page_xls else # render normally end end def save_page_xls #TRESCLOUD - we create a proper name for the file path = URI(request.referer).path.gsub(/[^0-9a-z]/i, '') query = URI(request.referer).query.gsub(/[^0-9a-z]/i, '') filename = @project_data['NOMBRE']+"_"+path+"_"+query+".xls" #TRESCLOUD - we render the page into a variable and process it page = render_to_string #TRESCLOUD - we send the file for download! send_data(page, :filename =&gt; filename, :type =&gt; "application/xls") end </code></pre> <p>Thanks for your tips!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload