How to define Rails Helper for one specific action?
I have a LabelTagHelper with a custom label_tag
method, but I only need this me开发者_StackOverflow社区thod in my new, create, edit and update actions.
Is there a way to define this helper only for a specific action? Something like helper :label_tag, :only => [:new, :create, :update, :destroy]
? Or it is safe to call self.class.helper :label_tag
in a before_filter?
rails Helpers should be just formatters. Think of it just as functions that will decorate your presentation layer (view)
So having helpers as :before_filter doesn't make any sense. As controllers actions often involves some server side processing.
So call your helper as and when you want. So there is no need to add helpers only for a specific action. as I mentioned earlier helpers and controller/ model actions should be mutually exclusive
BTW, could you elaborate your requirement, why you want to restrict your helper
HTH
sameera
try this,
Have a helper method like this (in my case i used a helper called 'ProjectsHelper')
module ProjectsHelper
def label_tag *params
if controller.action_name == "index"
#user custom implementation
else
super
end
end
end
This is working... hope this is what you need,
cheers
HTH
sameera
NOTE: sorry for not updating the existing answer. Since this is completely new approach thought of having it as a separate answer
精彩评论