Hi all,
I am trying to DRY some code in some of my controllers and keep getting
an undefined method error. Details as follows:
Some controllers dynamically generate methods inside a loop using
define_method. E.g.:
[‘shipment_product’, ‘transport’].each do |object_name|
sub_model_class = object_name.camelize.constantize
define_method(“list_#{object_name}”) do
@sub_model = sub_model_class.new
render ‘shared/list_sub_model’
end
…
end
I tried to move this code to application.rb as separate methods and call
the methods from each controller. E.g.:
in application.rb:
def sub_model_list_for(object_name)
sub_model_class = object_name.camelize.constantize
define_method(“list_#{object_name}”) do
@sub_model = sub_model_class.new
render ‘shared/list_sub_model’
end
end
in the controller:
[‘shipment_product’, ‘transport’].each do |object_name|
sub_model_list_for(object_name)
…
end
However if I do this, upon loading the controller I get undefined method
error for sub_model_list_for. (same thing happens in the console if I
‘load whatever_controller.rb’).
Any ideas? This code is actually in two controllers but probably will
generate some others that might use this code and I don’t want to repeat
the beast all over the place.
Thanks in advance,
Atha