Rails单击下载按钮

时间:2016-08-01 06:22:15

标签: ruby-on-rails csv

我是铁杆新手。我正在开发一个包含添加客户的应用程序。我有下载按钮。当我点击下载按钮它应该下载csv文件中的当前客户页面。

控制器

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_(路径)它显示模板错误,没有路由匹配错误点击下载按钮。有人请帮助我。我&#39 ;在这里附加了输出链接。提前谢谢!!

Customer_details.csv

3 个答案:

答案 0 :(得分:0)

在控制器的更新方法中,更改以下行:

import { Pipe, PipeTransform, EventEmitter } from '@angular/core';

import { GlobalService } from './global-service'
import { MyInterface } from './my-interface'

@Pipe({name: 'myPipe'})
export class MyPipe implements PipeTransform {

    private value: string;

    private _interface: MyInterface;
    private interfaceChanged: EventEmitter<MyInterface>;

    constructor(private globalService: GlobalService) {

        this._interface = globalService._interface;
        this.interfaceChanged = this.globalService
                                   .interfaceChanged
                                   .subscribe((newInterface: MyInterface) => {
                                        this._interface = newInterface;
                                   });
    }

    transform(value: string, args: any[]): string {
        for (var key in this.language) {
            if (key == value) {
                this.value = this._interface[key];
                break;
            }
        }
        return this.value;
    }
}

答案 1 :(得分: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会返回所有用户的数据。

答案 2 :(得分:0)

<强>模型

node["Name"]

<强>控制器

NullReferenceException

查看

def to_csv
    CSV.generate do |csv|
      csv << self.class.column_names
      csv << self.attributes.values_at(*self.class.column_names)
      end
    end
我在控制器中添加了单独的动作方法,并改变了方法和路线。最终得到了答案