0

次のようなリクエストパラメータに応じて、コントローラにミックスインを動的に追加しようとしています:

# Controller
class QuantitiesController < Admin::BaseController
  before_filter :extend_input_method, only: [:create, :new]
  def extend_input_method
    input_method = params[:input_method]
    if input_method
      send(:extend, "InputMethod::#{input_method.classify}".constantize)
    end
  end
end

# Mixin that gets included in the controller
module InputMethod::Single
  include InputMethod::Helpers

  def new
    puts "CALLED #new" # Debug information
    load_recent_entries
    quantity
  end

  def create
    @quantity = scoped_by_subject.new(process_attributes)

    if @quantity.save
      save_success
    else
      load_recent_entries
      save_error
    end
  end
end

メソッドが呼び出されることはありませんが、インスタンスを拡張した後であってもnew、例外を発生させることなくテンプレートがレンダリングされます。action_namenewrespond_to?("new")true

これが機能しない理由と、同様のことを実現する方法を理解したいと思います。

4

1 に答える 1

0

これが私が思いついた解決策です。それは私のニーズに合っています。

class QuantitiesController < Admin::BaseController
  before_filter :extend_input_method, only: [:create, :new]

  def new
    _new
  end

  def create
    _create
  end

  private
  def extend_input_method
    input_method = params[:input_method]
    extend(Dep.get("InputMethod::#{input_method.classify}")) if input_method
  end

end


module InputMethod::Single
  include InputMethod::Helpers

  def _new
    # Do stuff...
  end

  def _create
    # Do stuff...
  end

end
于 2012-09-16T21:41:33.430 に答える