2013-10-28 87 views
0

我試圖將導出列表導出爲csv。我使用daterangepicker來選擇日期,然後使用JQuery導出列表,但每當我按下導出按鈕的頁面刷新...在調試模式,我可以看到它通過代碼,但在結束的時候功能沒有任何反應......csv與jQuery導出

這裏是我的CoffeeScript代碼

# 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 

我希望有人能幫助... ...

回答

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 %>