Rails中的LocalJumpError(No Block Given)

时间:2010-07-22 13:42:07

标签: ruby-on-rails ruby

此方法适用于我的本地计算机,但在远程服务器上不起作用。它正在寻找一个区块,但我不知道“在哪里”或“如何”放置它。

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

错误:

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)

更新

如果在此控制器的焦点内任何地方调用will_paginate,它将运行此函数。

Finder.rb第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

由于某种原因,这个函数和我上面写的函数不匹配。如果我可以解决这个问题,那么这张票可以关闭。

1 个答案:

答案 0 :(得分:6)

method_missing_without_paginate方法来自alias_method_chain。 will_paginate会在Active Record中的method_missing默认行为之前添加行为。

我猜这个错误是在这一行(这是第62行)

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

关键位:card_signups.each.reject {

您的本地计算机上可能有1.8.7。在1.8.7中,没有块的each返回一个可以迭代的Enumerator对象。这是从1.9反向移植的,非常棒。

但是,1.8.6并没有这样做。在1.8.6中,each需要一个块才能工作。你没事,因为你不需要这个表达式中的each

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

它应该有用。

要考虑的另一件事是card_signups会有多大。您可能希望使用:conditions告诉数据库为您进行过滤。

假设您使用名为envelope的布尔列,它将如下所示:

@signups = CardBatch.find(params[:id]).card_signups.all :conditions => {:envelope=>false}