5

I'm trying export data using active record and csv class, but I dont have the file generated can help see me code.

when i click export does not open a dialog to download from csv am i missing something?

My Controller

def export_csv
    @contact = Contacts.find_by_sql("select * from contacts_list limit 10")
     respond_to do |format|
       format.html
       format.csv { send_data @contact.as_csv }
     end
  end

My Model

def self.to_csv
    attributes = %w{id name mail}

    CSV.generate(headers: true) do |csv|
      csv << attributes

      all.each do |contact|
        csv << attributes.map{ |attr| contact.send(attr) }
      end
    end
  end

My View

<%= link_to( 'Export CSV' ,{ :controller => :company, :action => :export_csv, format: "csv"}, { :class => "btn-ex" } ) %>

Console Ouput

Started GET "/company/export_cs" for 127.0.0.1 at 2019-12-17 11:31:26 -0200
Processing by CompanyController#export_cs as HTML

only this nothing more no errors no messages warnings.

5
  • What is your question? Commented Dec 17, 2019 at 14:16
  • when i click export does not open a dialog to download from csv am i missing something? Commented Dec 17, 2019 at 14:26
  • After clicking on the link go to your web server console and copy the request and response info. Add it to your question, don't paste it in the comments. Commented Dec 17, 2019 at 14:30
  • @Beartech updated post dialog is not open to save csv file.. Commented Dec 17, 2019 at 14:40
  • @mrzasa updated post see Commented Dec 17, 2019 at 14:41

2 Answers 2

4

You need to generate the URL for the link in a different way - using URL helper, not controller/action params:

<%= link_to( 'Export CSV', companies_export_csv_path(format: "csv", { :class => "btn-ex" } ) %>

See also: Rails link_to :format => :xlsx not generating link to .xlsx path

Sign up to request clarification or add additional context in comments.

1 Comment

Small correction. Right side bracket is missing. This can be written as below <%= link_to 'Export CSV', companies_export_csv_path(format: "csv"), { :class => "btn-ex" } %>
1
<%= link_to( 'Export' ,{ :controller => :company, :action => :export_csv, format: 'csv', :company_id => @company.id}, { :class => "btn-edit" } ) %>

Thanks Guys, workperfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.