2016-08-01 12 views
1

Ich bin neu bei Schienen. Im Entwickeln einer App, die das Hinzufügen von Kunden enthält.In, dass ich Download-Button habe. Wenn ich auf Download-Button klicken Es sollte die aktuelle Kundenseite in CSV-Datei herunterladen.Schienen, wie nur aktuellen Datensatz herunterladen, wenn geklickt Download-Taste

-Controller

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 

Ansicht

.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") 

Das Problem ist es aus der Form alle Datensätze herunterlädt. Ich weiß nicht, ob zur Aktion in Update oder Edit gegeben werden soll. Wenn ich den Pfad edit_customer_detail anstelle von customer_details_ (Pfad) gibt es Vorlage Fehler und keine Route entspricht Fehler beim Klicken auf download button.can jemand bitte helfen Sie mir.Ich habe habe den Output-Link hier angehängt. Danke im Voraus!!

Customer_details.csv

Antwort

0

Modell

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

Controller

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

Ansicht

.col-sm-3 
 
       - if @customer_detail.save 
 
       = link_to "Download", download_csv_customer_detail_path(@customer_detail.id, format: "csv"), class: "btn btn-primary"
ich separate Methode der Aktion in der Steuerung und geänderte Methode und routes.finally bekam

0

In Update-Methode der Steuerung, Änderung unter der Linie:

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

verändert eine Antwort hinzugefügt haben! .Es zeigt mir Vorlage fehlt Fehler, wenn ich customer_details_path geben. Und keine Route stimmt überein, wenn ich edit_customer_detail gebe. Kannst du mir weiter helfen? –

+0

= link_to "Download", customer_details_path (format: "csv"), klasse: "btn btn-primary" –

+0

wo muss ich in meinem code ersetzen? .in form? oder Controller ?. Kannst du kleine Details erklären –

0

In application.rb hinzufügen require 'csv'

Ändern Sie Ihre Update-Funktion zu

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 

Sie waren dow nload-Bericht für alle Benutzer, da @customer_details = CustomerDetail.all Daten für alle Benutzer zurückgibt.

+0

Ich habe bereits die Anforderung 'csv' hinzugefügt. So lädt es, aber nicht aktuelle Benutzerform. Es lädt alle Benutzerdatensätze herunter. Es zeigt mir Vorlage fehlt Fehler, wenn ich customer_details_path gebe.Und keine Route stimmt überein, wenn ich edit_customer_detail gebe. Vielen Dank!. Kannst du mir weiter helfen? –

+0

@HariPrakash Sie haben keine passende Route für edit_customer: /customers/:id/edit(.:format) Kunden # bearbeiten. Können Sie eine Funktion und Route erstellen, um Kundendetails zu bearbeiten? –