Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:43:16+00:00 2026-05-26T21:43:16+00:00

Class UserController def export_users users = User.all stream_csv do |csv| csv << [Name,Email,Gender] users.each

  • 0
Class UserController
  def export_users
    users = User.all
    stream_csv do |csv|
      csv << ["Name","Email","Gender"]
      users.each do |i|
        csv << [i.name,i.email,i.gender]
      end
    end
  end

def stream_csv
    require 'fastercsv'

        filename = params[:action] + ".csv"

        #this is required if you want this to work with IE      
        if request.env['HTTP_USER_AGENT'] =~ /msie/i
            headers['Pragma'] = 'public'
            headers["Content-type"] = "text/plain"
            headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
            headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
            headers['Expires'] = "0"
        else
            headers["Content-Type"] ||= 'text/csv'
            headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
            controller.response.headers["Content-Transfer-Encoding"] = "binary"
        end

        render :text => Proc.new { |response, output|
            csv = FasterCSV.new(output, :row_sep => "\r\n")
            yield csv
        }
    end
end

Err: “#Proc:0x9382539@/sites/app/controllers/export_controller.rb:56”

Using Ruby 1.8 and Rails 3.0.9

So I think the problem here is that I’m not using “Proc” right. Or it’s not supposed to act like just another block…

I thought about programming a new logic into the class so that reads better. But if somebody could explain to me why my code is wrong or at least point me in a new direction than I might be able to learn something new here. Thanks

Note: Found a better way:

def export_inverts
  require 'fastercsv'
  inverts = Invert.all
  filename = params[:action] + ".csv"

  #this is required if you want this to work with IE
  if request.env['HTTP_USER_AGENT'] =~ /msie/i
    headers['Pragma'] = 'public'
    headers["Content-type"] = "text/plain"
    headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
    headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
    headers['Expires'] = "0"
  else
    headers["Content-Type"] ||= 'text/csv'
    headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
    headers["Content-Transfer-Encoding"] = "binary"
  end

  csv_string = FasterCSV.generate do |csv|
    csv << ["Genus","Species","Common Name","Pet Name","Gender"]
    inverts.each do |i|
      csv << [i.scientific_name,i.scientific_name,i.common_name,i.pet_name,i.gender]
    end
  end
  render :text => csv_string
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:43:16+00:00Added an answer on May 26, 2026 at 9:43 pm

    Yield can only be used inside a function or a block. Yield is used in a function that takes a block to say, yield some value into the block. Actually it says yield this value into the proc that the block has been converted into with the ampersand operator (in most cases). However, you could pass a Proc to a function that was expecting it.

    Here, you just want to return the value from the proc and “yield” isn’t needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose this is my users controller:- class UsersController < ApplicationController def show @user =
I'm using pylons, and my action of controller is: class UserController(BaseController): def create(self): name
class UsersController < ApplicationController def edit ... end Mu routes.rb match '/user/:id/profile/edit', :to =>
I'm on Rails 2.3.5. In a typical user controller create action class UsersController def
I have a user controller which consists of a method named listfolders(). class UserController
I have users, posts and comments. User can post only one comment to each
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,
I have this setup: class UsersController < InheritedResources::Base respond_to :html, :js, :xml, :json def
I have two types of users: managers, and employees. I use one User controller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.