Rails how to download only current record when clicked download button

Hari Prakash

I'm new to rails. Im developing an app which contains adding customers.In that I have download button.When I click download button It should download the current customer page in csv file.

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

View

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

The problem is it downloads all the records from of the form. I dont know whether to action is to be given in update or edit.If i give the path edit_customer_detail instead of customer_details_(path) it shows template error and no route matches error when clicking download button.can someone please help me.I've attached the output link here. thanks in advance!!

Customer_details.csv

Hari Prakash

model

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

view

.col-sm-3
              - if @customer_detail.save
                = link_to "Download", download_csv_customer_detail_path(@customer_detail.id, format: "csv"), class: "btn btn-primary"
I've added seperate method of action in controller and changed method and routes.finally got an answer

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to cancel a download when user clicked in android

From Dev

How can I let a user download multiple files when a button is clicked?

From Dev

how to display a div only when a button is clicked

From Dev

How to get file name and url file when clicked in browser for download

From Dev

jspdf download button doesn't do anything when clicked. in android cordova

From Dev

Mutt: download only current folder using offlineimap

From Java

How to trigger a file download when clicking an HTML button or JavaScript

From Dev

How to cancel a download request when cancel button pressed in swift

From Dev

C# How to trigger a file download when clicking a button?

From Dev

How to create a black curtain that only appears when a button is clicked?

From Dev

How to perform action for a menu item only when button is clicked

From Dev

How to make only one Button selected when clicked -QUIZ ANDROID

From Dev

how to make only 1 new window when a button is clicked? tkinter

From Dev

jQuery: How to enable button when only all check boxes are clicked?

From Dev

How can I change the color of a button with CSS only when it is clicked?

From Dev

How to disable the download button in ViewerJS?

From Dev

How to add a download button on Fancybox?

From Dev

Button to be able to input current time when clicked

From Dev

How to turn an svg button into a download button?

From Dev

Disable button when download is in progress Angular 4

From Dev

How to download the current document's innerHTML as a file?

From Dev

How In geb download zip file into the current directory?

From Dev

How to get the current upload and download speeds in terminal?

From Dev

How to get the current upload and download speeds in terminal?

From Dev

How to download several png plots within one download button in shiny

From Dev

How to automatically download the files that have a download button on a webpage?

From Dev

How to download from GitHub without download zip button?

From Dev

How to automatically download the files that have a download button on a webpage?

From Dev

How to disable a button when clicked?

Related Related

  1. 1

    how to cancel a download when user clicked in android

  2. 2

    How can I let a user download multiple files when a button is clicked?

  3. 3

    how to display a div only when a button is clicked

  4. 4

    How to get file name and url file when clicked in browser for download

  5. 5

    jspdf download button doesn't do anything when clicked. in android cordova

  6. 6

    Mutt: download only current folder using offlineimap

  7. 7

    How to trigger a file download when clicking an HTML button or JavaScript

  8. 8

    How to cancel a download request when cancel button pressed in swift

  9. 9

    C# How to trigger a file download when clicking a button?

  10. 10

    How to create a black curtain that only appears when a button is clicked?

  11. 11

    How to perform action for a menu item only when button is clicked

  12. 12

    How to make only one Button selected when clicked -QUIZ ANDROID

  13. 13

    how to make only 1 new window when a button is clicked? tkinter

  14. 14

    jQuery: How to enable button when only all check boxes are clicked?

  15. 15

    How can I change the color of a button with CSS only when it is clicked?

  16. 16

    How to disable the download button in ViewerJS?

  17. 17

    How to add a download button on Fancybox?

  18. 18

    Button to be able to input current time when clicked

  19. 19

    How to turn an svg button into a download button?

  20. 20

    Disable button when download is in progress Angular 4

  21. 21

    How to download the current document's innerHTML as a file?

  22. 22

    How In geb download zip file into the current directory?

  23. 23

    How to get the current upload and download speeds in terminal?

  24. 24

    How to get the current upload and download speeds in terminal?

  25. 25

    How to download several png plots within one download button in shiny

  26. 26

    How to automatically download the files that have a download button on a webpage?

  27. 27

    How to download from GitHub without download zip button?

  28. 28

    How to automatically download the files that have a download button on a webpage?

  29. 29

    How to disable a button when clicked?

HotTag

Archive