使用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
我做错了什么?感谢。
答案 0 :(得分:5)
define_method
需要阻止,您错过了do
define_method