Hi, To avoid cluttering views, the docs recommand creating a helper module, and doing an include in the controller. To make the methods available to the view it recommends: def self.append_features(controller) #:nodoc: controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super end With this procedure, the controller itself does _not_ include the module, even though the include occurs in the controller class. It''s useful to have then available in the controller class as well. I propose to do: def self.append_features(controller) #:nodoc: if controller.ancestors.include?(ActionController::Base) controller.add_template_helper(self) end super end This way the helper gets included in the controller. Cheers, Han Holl
Couldn''t that cause problems if someone called the helper method as an action within the URL? There''s probably a way around that issue, but it could still present a problem. -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
On Friday 05 November 2004 16:13, John Wilger wrote:> Couldn''t that cause problems if someone called the helper method as an > action within the URL? There''s probably a way around that issue, but > it could still present a problem.I hadn''t realized that. Yes, it might, maybe. But what about istance methods in other ancestors? DpsController < AbstractApplicationController < ActionController::Base < ActionController::Scaffolding < ActionController::Rescue < ActionController::Benchmarking < ActionController::Flash < ActionController::Layout < ActionController::Filters::InstanceMethods < ActionController::Filters < ClassInheritableAttributes < Object < TMail < Kernel Han Holl
Han Holl wrote:> With this procedure, the controller itself does _not_ include the module, even > though the include occurs in the controller class. It''s useful to have then > available in the controller class as well. I propose to do:I agree it is confusing that ''include FooHelper'' does not actually include FooHelper''s methods. I propose a controller class method called ''helper'' to declare view helpers. This makes it clear that these methods are for the view and not for the controller. I think this is a valuable distinction that helps to clarify your application code. Before: foo_helper.rb ---- module FooHelper def self.append_features(controller) #:nodoc: controller.ancestors.include?(ActionController::Base) ? controller.add_template_helper(self) : super end def foo "What you lookin at, foo?" end end foo_controller.rb ---- require ''abstract_application'' require ''foo_helper'' require ''bar_helper'' require ''foo'' class FooController < AbstractApplicationController include FooHelper include BarHelper scaffold :bar end After: foo_helper.rb ---- module FooHelper def foo "What you lookin at, foo?" end end foo_controller.rb ---- require ''abstract_application'' require ''bar'' class FooController < AbstractApplicationController helper :foo, :bar scaffold :bar end The patch is at http://dev.rubyonrails.org/trac.cgi/ticket/170 Best, jeremy
On Friday 05 November 2004 18:39, Han Holl wrote:> I hadn''t realized that. Yes, it might, maybe. But what about istance > methods in other ancestors? > > DpsController < AbstractApplicationController < ActionController::Base < > ActionController::Scaffolding < ActionController::Rescue < > ActionController::Benchmarking < ActionController::Flash < > ActionController::Layout < ActionController::Filters::InstanceMethods < > ActionController::Filters < ClassInheritableAttributes < Object < TMail < > Kernel >To follow up on that, here''s a list of _valid_ action methods, that will actually be send to the controller class if someone types them in as a URL: action_name active_layout after_action assigns assigns= before_action call_consider_all_requests_local call_ignore_missing_templates call_logger call_template_class call_template_root call_view_controller_internals consider_all_requests_local consider_all_requests_local= controller_class_name controller_name cookie cookies cookies= default_url_options display flash headers headers= ignore_missing_templates ignore_missing_templates= keep_flash lang list local_request? log_error logger logger= method_missing new nextrapport notempty? params params= perform_action_with_benchmark perform_action_with_filters perform_action_without_benchmark perform_action_without_rescue process process_cgi redirect_to redirect_to_path redirect_to_url render render_action render_file render_template render_text render_with_benchmark render_with_layout render_without_benchmark render_without_layout request request= rescue_action rescue_action_in_public rescue_action_locally reset_session response response= rewrite_options send_file session session= sql template_class template_class= template_root template_root= url_for view_controller_internals view_controller_internals Most of them probably will give weird looking error messages like: No rhtml or rxml template found for dps/cookies with traceback and the works. Maybe some kind of scheme with helper methods protected, and actions being public could help. Cheers, Han Holl