title: How to define a custom view to generate a xlsx file in Rails 5.2 for downloading?
body: <p>In my ParametersController, I defined the <strong>export</strong> method to download a list of parameters as a xlsx formated file:</p>
  <pre><code>  # Get /parameters_list/1/export.xlsx
    def export
      ### Exports the parameters attached to @parameters_lists in a Workbook
      workbook = WriteXLSX.new(&quot;./public/#{@parameters_list.code}_parameters.xlsx&quot;)
      title = workbook.add_format(:font  =&gt; 'Calibri', :size  =&gt; 16, :color =&gt; 'blue', :bold  =&gt; 1)
      header = workbook.add_format(:font  =&gt; 'Calibri', :size  =&gt; 12, :color =&gt; 'black', :bold  =&gt; 1)
      data = workbook.add_format(:font  =&gt; 'Calibri', :size  =&gt; 10, :color =&gt; 'black', :bold  =&gt; 0)
      workbook.set_properties(
      :title    =&gt; &quot;Parameters lists export: #{@parameters_list.code}&quot;,
      :subject  =&gt; 'This file contains exported parameters',
      :author   =&gt; 'Open Data Quality',
      :comments =&gt; translation_for(@parameters_list.name_translations)
      #,
      #:hyperlink_base =&gt; url_for @parameters_list
      )
  
      ### write Description sheet
      cover = workbook.add_worksheet('Parameters list')
      cover.write(0, 0, @parameters_list.code, title)
      cover.write(1, 0, translation_for(@parameters_list.name_translations), header)
      index = 3
      @parameters_list.attributes.map do |name, value|
        cover.write(index, 0, name, header)
        cover.write(index, 1, value, data)
        index += 1
      end
  
      ### write Data sheet
      data_sheet = workbook.add_worksheet('Parameters')
      index = 0
  
      ## Headers
      # Write attributes headers
      @parameters_list.parameters.first.attributes.each do |attribute, value|
        data_sheet.write(0, index, attribute, header)
        index += 1
      end
  
      # Add translations headers
      list_of_languages.order(:property).each do |locution|
        data_sheet.write(0, index, &quot;Name_#{locution.code}&quot;, header)
        index += 1
        data_sheet.write(0, index, &quot;Description_#{locution.code}&quot;, header)
        index += 1
      end
  
      ## Data
      @parameters_list.parameters.each_with_index do |parameter, line_index|
        index = 0
        # Write attributes
        parameter.attributes.each do |attribute, value|
          data_sheet.write(line_index + 1, index, value, data)
          index += 1
        end
  
        # Add translations
        list_of_languages.order(:property).each do |locution|
          data_sheet.write(line_index + 1, index, parameter.name_translations.where(language: locution.property).first&amp;.translation, data)
          index += 1
          data_sheet.write(line_index + 1, index, parameter.description_translations.where(language: locution.property).first&amp;.translation, data)
          index += 1
        end
  
      end
  
      # close workbook
      workbook.close
  
      send_file &quot;./public/#{@parameters_list.code}_parameters.xlsx&quot;, type: &quot;application/xlsx&quot;
  
    end
  </code></pre>
  <p>With respect to the MVC architecture, I think this would rather be defined as a view, accessible through a URL with specified format, such as <code>my_server/parameters_lists/15.xlsx</code> relying on the show method:</p>
  <pre><code>  # GET /parameters_list/1
    # GET /parameters_list/1.json
    # GET /parameters_list/1.csv
    # GET /parameters_list/1.xlsx
    def show
      ### @parameters_list retrieved by callback function
  
      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @parameters_list.parameters }
        format.csv { send_data @parameters_list.parameters.to_csv }
        format.xlsx # uses specific template to generate the a file
      end
    end
  </code></pre>
  <p>I tried several ways to move and encapsulate the code into a show.xlsx.erb file, but it does not generate
  more than an text file without data.</p>
  <p>Can you help me find out how to achieve this?</p>