0

使用 Rails 3.2。下面是我的控制器:

class ShopsController < ApplicationController
  ...

  class << self
    Shop::SHOP_TYPES.each do |shop_type|
      define_method "nearby_#{shop_type.pluralize}"
        @nearby_type = "#{shop_type.pluralize}"
      end
    end
  end

 ...
end

class Shop < ActiveRecord::Base  
  SHOP_TYPES =  %w(cafe restaurant)
end

然而,它给了我syntax error, unexpected keyword_end, expecting $end最后的end归属class ShopsController < ApplicationController。我试图保持代码干燥,这样我就不必手动编码:

class ShopsController < ApplicationController
  ...

  def nearby_cafes
    @nearby_type = "cafes"
  end

  def nearby_restaurants
    @nearby_type = "restaurants"
  end

 ...
end

我做错了什么?谢谢。

4

1 回答 1

5

define_method需要一个块,你错过了do你打电话的那一行define_method

于 2012-10-22T16:34:18.830 回答