I\'m writing a model that handles user input from a text area. Following the advice from http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input, I\'m cleani
This works better for me:
Simple:
ApplicationController.helpers.my_helper_method
Advance:
class HelperProxy < ActionView::Base
include ApplicationController.master_helper_module
def current_user
#let helpers act like we're a guest
nil
end
def self.instance
@instance ||= new
end
end
Source: http://makandracards.com/makandra/1307-how-to-use-helper-methods-inside-a-model
To access helpers from your own controllers, just use:
OrdersController.helpers.order_number(@order)
Just change the first line as follows :
include ActionView::Helpers
that will make it works.
UPDATE: For Rails 3 use:
ActionController::Base.helpers.sanitize(str)
Credit goes to lornc's answer
I wouldn't recommend any of these methods. Instead, put it within its own namespace.
class Post < ActiveRecord::Base
def clean_input
self.input = Helpers.sanitize(self.input, :tags => %w(b i u))
end
module Helpers
extend ActionView::Helpers::SanitizeHelper
end
end
If you want to use a the my_helper_method
inside a model, you can write:
ApplicationController.helpers.my_helper_method
This gives you just the helper method without the side effects of loading every ActionView::Helpers method into your model:
ActionController::Base.helpers.sanitize(str)