使用csv格式生成表单

问题描述 投票:0回答:1

我正在尝试使用form来指定两个日期字段,它指定我想要放入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
1个回答
1
投票

:url冲突:格式如果你同时传递:url和:format,url会覆盖格式的使用,所以你需要在url中传递它,如下所示:

form_for user, :url => user_path(@user, :format => :json)

Source

© www.soinside.com 2019 - 2024. All rights reserved.