0

我正在尝试将 Rails 上的列表导出为 csv。我正在使用 daterangepicker 选择日期,然后使用 JQuery 导出列表,但是每当我按下导出按钮时,页面就会刷新......在调试模式下,我可以看到它通过代码,但在结束时功能什么都没有发生...

这是我的咖啡脚本代码

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

if gon.current_controller == 'measurements'
  $ ->
    # Search functionality
    $('form.measurement.search [name=daterange]').daterangepicker({
        format: gon.js_formats.moment.date,
        ranges: {
           'Today': [moment(), moment()],
           'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
           'Last 7 Days': [moment().subtract('days', 6), moment()],
           'This Week': [moment().startOf('week'), moment().endOf('week')],
           'Last Week': [moment().subtract('week', 1).startOf('week'), moment().subtract('week', 1).endOf('week')],
           'Last 30 Days': [moment().subtract('days', 29), moment()],
           'This Month': [moment().startOf('month'), moment().endOf('month')],
           'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
        },
        startDate: moment().subtract('days', 29),
        endDate: moment()
      },
      (start, end) -> $('form.measurement.search').submit());

    $('form.measurement input,select').change (ev) ->
      $('form.measurement.search').submit()

    $('form.measurement [name=clear]').click (ev) ->
      $('form.measurement.search [name=daterange]').val('')
      $('form.measurement.search').submit()
      false

    # CSV export
    $('form.measurement [name=format_csv]').click (ev) ->
      form = $(ev.target).closest('form')
      $('[name=format]', form).val('csv').prop('disabled', false)
      form.submit()
      $('[name=format]', form).val('').prop('disabled', true)
      false

这是我调用函数的方式:

<%= submit_tag "Export CSV", name: :format_csv, class: 'btn btn-default' %> 

这是我的控制器:

class MeasurementsController < ApplicationController
  # GET /measurements
  include MeasurementsHelper
  def index
    @channel_name = Channel.find(params[:channel_id]).try(:label)
    @measurements = filter_measurements_by_params(Measurement.where(channel_id: params[:channel_id]).order('measured_at DESC'))
     @users = User.select(:id, :username, :name)

    @total =  @measurements.length
    @measurements = @measurements.paginate(page: params[:page], per_page: 50)

    respond_to do |format|
      format.html # index.html.erb
      format.csv { render csv: @measurements }
    end
  end

  def show
    @measurement = Measurement.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @measurement }
    end
  end

  # GET /measurements/new
  def new
    @measurement = Measurement.new(channel_id: params[:channel_id])

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  # POST /measurements
  def create
    params[:measurement][:user_id] = current_user.id
    params[:measurement][:channel_id] = params[:channel_id]
    @measurement = Measurement.new(measurement_params_create)

    respond_to do |format|
      if @measurement.save
        format.html { redirect_to channel_measurements_url(@measurement.channel), notice: 'Measurement was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end

  # DELETE /measurements/1
  def destroy
    @measurement = Measurement.find(params[:id])
    channel = @measurement.channel
    @measurement.destroy

    respond_to do |format|
      format.html { redirect_to channel_measurements_url(channel), notice: 'Measurement was successfully deleted.' }
    end
  end

  private
    def measurement_params_create
      params.require(:measurement).permit(:value, :channel_id, :user_id, :measured_at)
    end
end

我希望有人可以帮助...

4

1 回答 1

0

我找到了解决方案...

我忘了将隐藏字段标签添加到 html 中。

 <%= hidden_field_tag 'authenticity_token', form_authenticity_token.to_s, disabled: true %>
    <%= hidden_field_tag 'format', nil, disabled: true %>
    <%= hidden_field_tag 'description', nil, disabled: true %>
于 2013-11-04T14:21:03.533 回答