Skip to main content

Add View Option to view csv

Rails • Asked by Navroop Singh Sidhu

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 

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.