2016-08-01 62 views
1

我是新來的鐵軌。我正在開發一個包含添加客戶的應用程序。在那裏我有下載按鈕。當我點擊下載按鈕時它應該在csv文件中下載當前客戶頁面。Rails如何只下載當前記錄點擊下載按鈕

控制器

def create 
    @customer_detail = CustomerDetail.new(customer_detail_params) 
    @customer_detail.company_profile_id = current_user.company_profile.id 
    respond_to do |format| 
    if @customer_detail.save 
     format.html { redirect_to edit_customer_detail_path(@customer_detail), notice: 'customerDetails was successfully created.' } 
     # format.html { render 'edit', notice: 'customerDetails was successfully created.' } 
    else 
     format.html { render :new } 
    end 
    end 
end 

def index 
    @customer_details = CustomerDetail.all 
end 

def destroy 
end 

def update 
    respond_to do |format| 
    format.html 
    format.csv { render text: @customer_details.to_csv } 
    if @customer_detail.update(customer_detail_params) 
     format.html { redirect_to @customer_detail, notice: 'customer_details was successfully updated.' } 
    else 
     format.html { render :edit } 
    end 
    end 
end 

查看

.fieldset 
    .row 
    .col-sm-3 
     = f.submit "Save", class: "btn btn-primary" 
    .col-sm-3 
     = f.submit "cancel", type: :reset, class: "btn btn-primary" 
    .col-sm-3 
     = link_to "Download", edit_customer_detail(format: "csv"), class: "btn btn-primary" 
    .col-sm-3 
     = link_to("Print", "javascript:print()", class: "btn btn-primary") 

問題是,它從下載的形式的所有記錄。我不知道是否要更新或編輯行動。如果我給路徑edit_customer_detail而不是customer_details_(路徑)它顯示模板錯誤,並單擊下載按鈕時沒有路徑匹配錯誤。有人請幫助我。我是在這裏附上輸出鏈接。提前致謝!!

Customer_details.csv

回答

0

模型

def to_csv 
 
    CSV.generate do |csv| 
 
     csv << self.class.column_names 
 
     csv << self.attributes.values_at(*self.class.column_names) 
 
     end 
 
    end

控制器

def download_csv 
 
    respond_to do |format| 
 
     format.html 
 
     format.csv { render text: @customer_detail.to_csv } 
 
    end 
 
    end

視圖

.col-sm-3 
 
       - if @customer_detail.save 
 
       = link_to "Download", download_csv_customer_detail_path(@customer_detail.id, format: "csv"), class: "btn btn-primary"
我已在控制器和改變方法的行動獨立的方法和routes.finally得到的回答

0

在控制器的更新方法,下面一行變化:

format.csv { render text: @customer_details.to_csv } with 
format.csv { render text: @customer_detail.to_csv } 
+0

改變!。它表明了我的模板丟失的錯誤,如果我給customer_details_path。如果我給edit_customer_detail,沒有路線匹配。你能進一步幫助我嗎? –

+0

= link_to「下載」,customer_details_path(格式:「csv」),類:「btn btn-primary」 –

+0

我必須在我的代碼中替換?.in表單?或控制器?你可以詳細解釋一下 –

0

在application.rb中 添加require 'csv'

更改您的更新功能

def update 
    respond_to do |format| 
    format.html 
    format.csv { render text: @customer_detail.to_csv } 
    if @customer_detail.update(customer_detail_params) 
     format.html { redirect_to @customer_detail, notice: 'customer_details was successfully updated.' } 
    else 
     format.html { render :edit } 
    end 
    end 

你是道瓊斯爲所有用戶下載報告,因爲@customer_details = CustomerDetail.all會爲所有用戶返回數據。

+0

我已經添加了require'csv'。所以它下載,但不是當前的用戶form.It下載所有用戶records.It顯示我的模板是缺少錯誤,如果我給customer_details_path。如果我給edit_customer_detail,沒有路線匹配。謝謝!。你能進一步幫助我嗎? –

+0

@HariPrakash您沒有edit_customer的匹配路由:/customers/:id/edit(.:format)customers#edit。你可以創建一個功能和路線來編輯客戶的詳細信息 –