【发布时间】:2018-03-01 13:55:16
【问题描述】:
我正在尝试使用表单来指定两个日期字段,这指定了我要放入 CSV 文件中的数据的时间跨度。我能够创建我的 csv 文件,但我无法让 rails 响应 csv 格式。
在这里,我在 form_for 字段中添加了我的格式: :csv,但它不会响应我的控制器中的该格式。
<%= form_for(:estimation, html: {class: "estimations-form-horizontal"}, url: research_path, format: :csv, method: :get) do |f| %>
<div class="form-group-estimations">
<%= f.label :from_start_date, "Från och med" %>
<%= f.date_field :from_start_date, class: "form-control" %>
</div>
<div class="form-group-estimations">
<%= f.label :to_end_date, "Till och med" %>
<%= f.date_field :to_end_date, class: "form-control" %>
</div>
<%= f.submit "Hämta data", class: "btn btn-primary" %>
<% end %>
我的控制器,使用 format.csv ...
def research
if (params[:estimation] && params[:estimation][:from_start_date] && params[:estimation][:to_end_date])
start_date = params[:estimation][:from_start_date].to_date.beginning_of_day
end_date = params[:estimation][:to_end_date].to_date.end_of_day
if(start_date > end_date)
puts "Dates are in wrong order"
else
@estimations = Estimation.where(:created_at => start_date..end_date)
puts to_csv(@estimations)
respond_to do |format|
format.csv { send_data to_csv(@estimations), filename: "skattningar-#{Date.today}.csv" }
end
end
else
puts "Not enough data"
end
end
【问题讨论】:
标签: ruby-on-rails csv