ruby - How to export models to a CSV file using Rails 3? -


i'd export database entries csv using rails 3.2. i've been following these instructions:

my controller: edit: i've changed class method instance method

... elsif params[:reference] == 'exportnews'   @data = news.find(:all, :order => "date_posted desc", :conditions => "#{settings.show} = 1", :limit => "5")   respond_to |format|     format.html { redirect_to root_url }     format.csv { send_data @data.to_csv }   end else ... 

my model:

  class news < activerecord::base       attr_accessible :content, :date_posted, :heading, :hidden, :reference, :title, :user       def to_csv(options = {})           csv.generate(options) |csv|             csv << column_names             all.each |product|               csv << product.attributes.values_at(*column_names).map(&:to_csv)             end           end         end     end 

yet when visit specified url (i've created route in config/routes.rb csv file following details:

#<news:0x007fef657d69f8> #<news:0x007fef632b95a8> #<news:0x007fef632b8ec8> #<news:0x007fef632b8680> #<news:0x007fef632b7e38>

what missing?

looks you're not calling method way (you're calling original .to_csv method instead. try doing in view:

format.csv { send_data @data.map(&:to_csv } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -