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 に答える