views:

152

answers:

1

I'm running this method, and this works perfectly on my local machine but does not work on my remote server. I get that it's looking for a block, but I'm not sure 'where' or 'how' to place it.

Here's my broken method:

def generate_csv
  if params[:print_envelopes]
    @signups = CardBatch.find(params[:id]).card_signups.each.reject { |a| a.envelope? }
    @filename = 'envelopes.csv'
    csv_string = FasterCSV.generate do |csv|
      csv << ["first name, last name, street address, city, state, zip code"]
      @signups.each do |signup|
        csv << [signup.first_name, signup.last_name, signup.street_address_1, signup.city, signup.state, signup.zip_code]
        signup.envelope = true
        signup.save
      end
    end
  elsif params[:print_cards]
    @signups = CardBatch.find(params[:id]).card_signups.reject { |a| a.card? }
    @filename = 'cards.csv'
    csv_string = FasterCSV.generate do |csv|
      csv << ["name", "expiration date", "card number"]
      @signups.each do |signup|
        csv << [signup.full_name, signup.exp_date.strftime('%m/%y'), signup.formatted_card_number] if card?
        signup.card = true
        signup.save
      end
    end
  end

  send_data(csv_string,
    :type => 'text/csv; charset=utf-8; header=present',
    :filename => @filename)
end

Is there something obvious I'm missing here?

The error:

Processing Admin::CardBatchesController#generate_csv (for 173.161.167.41 at 2010-07-21 07:19:47)     [POST]
  Parameters: {"action"=>"generate_csv",     "authenticity_token"=>"OcDII/t8ZleZxRBpISi+Giw+4MAV2Cjjq8bdixJJ+I8=", "id"=>"4", "controller"=>"admin/card_batches", "print_envelopes"=>"print envelopes"}

LocalJumpError (no block given):
  vendor/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing'
  app/controllers/admin/card_batches_controller.rb:62:in `generate_csv'
  haml (2.2.2) [v] rails/./lib/sass/plugin/rails.rb:19:in `process'
  lib/flash_session_cookie_middleware.rb:14:in `call'
  vendor/gems/hoptoad_notifier-2.2.2/lib/hoptoad_notifier/rack.rb:27:in `call'

** [Hoptoad] Failure: Net::HTTPClientError
** [Hoptoad] Environment Info: [Ruby: 1.8.6] [Rails: 2.3.3] [Env: production]
** [Hoptoad] Response from Hoptoad: 
<?xml version="1.0" encoding="UTF-8"?> 
<errors>
<error>No project exists with the given API key.</error>
</errors>
Rendering /data/HQ_Channel2/releases/20100721141730/public/500.html (500 Internal Server Error)

Update

Ok so I'm getting that if will_paginate is called anywhere within this controller's focus, it will run this function.

Finder.rb around line 170

  def method_missing_with_paginate(method, *args) #:nodoc:
    # did somebody tried to paginate? if not, let them be
    unless method.to_s.index('paginate') == 0
      if block_given?
        return method_missing_without_paginate(method, *args) { |*a| yield(*a) }
      else
        return method_missing_without_paginate(method, *args) 
      end
    end

    # paginate finders are really just find_* with limit and offset
    finder = method.to_s.sub('paginate', 'find')
    finder.sub!('find', 'find_all') if finder.index('find_by_') == 0

    options = args.pop
    raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
    options = options.dup
    options[:finder] = finder
    args << options

    paginate(*args) { |*a| yield(*a) if block_given? }
  end

And for some reason this function, and my function written above do not match up. If I can solve that, then this ticket can be closed.

+2  A: 

The method_missing_without_paginate method comes from alias_method_chain. will_paginate adds behavior before the default behavior of method_missing in Active Record.

I'm guessing that the error is on this line(that this is line 62)

@signups = CardBatch.find(params[:id]).card_signups.each.reject { |a| a.envelope? }

the key bit: card_signups.each.reject {

You probably have 1.8.7 on your local machine. In 1.8.7, each without a block returns an Enumerator object that you can iterate over. This was backported from 1.9 and is awesome.

But, 1.8.6 doesn't do that. In 1.8.6, each requires a block to work. You are ok though, because you don't need the each in this expression.

Replace that line with

@signups = CardBatch.find(params[:id]).card_signups.reject { |a| a.envelope? }

and it should work.

Another thing to think about would be how big card_signups will be. You might want to use :conditions to tell the db to do the filtering for you.

Assuming you are using a boolean column named envelope, it would look like this:

@signups = CardBatch.find(params[:id]).card_signups.all :conditions => {:envelope=>false} 
BaroqueBobcat
I didn't even test it yet, but I'm pretty sure you completely nailed this. MY server runs on 1.8.6, and my local is 1.8.7! Big tell right there.
Trip
I can't thank you enough BaroqueBobcat besides giving you this saying "If it ain't Baroque, don't fix it"
Trip