次のようなリクエストパラメータに応じて、コントローラにミックスインを動的に追加しようとしています:
# 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_name
new
respond_to?("new")
true
これが機能しない理由と、同様のことを実現する方法を理解したいと思います。