O
O
Oleg Bugrov2015-09-10 12:46:18
Ruby on Rails
Oleg Bugrov, 2015-09-10 12:46:18

Generating xsl file in rails from a form?


The essence of the task - there is a form, during the creation of which the user should receive an Excel document with the content of the form and a unique address name_date.xls link to get an Excel file with 1 form data. Now I get all the values ​​of forms. I've been stupid for a day, help pls.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TyzhSysAdmin, 2015-09-10
@mrakodav

If you do not need to save data anywhere and immediately give the XLS
Controller

class XlsController < ApplicationController
 # В index вьюшке выводим фарму
  def index
  end
  # Тут получаем POST запрос с данными формы и формируем XLS
  # routes.rb - post 'xls/get_xls'
  def get_xls
    filename = "form-#{Time.now.strftime("%Y%m%d%H%M%S")}.xls"
    send_data([validate_params].to_xls, :type => "text/xls; charset=utf-8; header=present", :filename => filename)
  end
  private
  # Валидация формы
  def validate_params
    params.require(:form).permit(:value1, :value2)
  end
end

View
<%= form_tag xls_get_path, method: "post" do %>
    <p><%= label_tag('VALUE1') %></p>
    <p><%= text_field_tag('form[value1]') %></p>
    <p><%= label_tag('VALUE2') %></p>
    <p><%= text_field_tag('form[value2]') %></p>
    <p> <%= submit_tag('SEND') %></p>
<% end %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question