All threads / Add View Option to view csv

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Add View Option to view csv

Navroop Singh Sidhu asked in Rails

I have added a button to download the CSV for the parent model. What I want is when a user clicks on Download CSV, user should be asked whether to DOWNLOAD or just VIEW the csv. How can I achieve this.
parent.rb

def self.to_csv
        attributes = %w{parent_1_firstname parent_1_lastname address city province telephone_number postal_code email family_situation created_at updated_at gross_income}
        CSV.generate(headers: true) do |csv|
            csv << attributes

            all.each do |parent|
                csv << parent.attributes.values_at(*attributes)
            end
        end
    end

Parent index.html.erb

<%= link_to "Download CSV",parents_path(@parent, format: :csv), class: "btn btn-primary btn-2x" %>

parent_controller

def index
    require 'csv'
    format.csv { send_data @parents.to_csv }
end 
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,699+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.