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 cleaning up the input in the model before saving to database, using the before_validate callback.
The relevant parts of my model look like this:
include ActionView::Helpers::SanitizeHelper
class Post < ActiveRecord::Base {
before_validation :clean_input
...
protected
def clean_input
self.input = sanitize(self.input, :tags => %w(b i u))
end
end
Needless to say, this doesn't work. I get the following error when I try and save a new Post.
undefined method `white_list_sanitizer' for #<Class:0xdeadbeef>
Apparently, SanitizeHelper creates an instance of HTML::WhiteListSanitizer, but when I mix it into my model it can't find HTML::WhiteListSanitizer. Why? What can I do about this to fix it?
This question is related to
ruby-on-rails
ruby
activerecord
model
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
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
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)
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)
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
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)
If you want to use a the my_helper_method
inside a model, you can write:
ApplicationController.helpers.my_helper_method
If you want to use a the my_helper_method
inside a model, you can write:
ApplicationController.helpers.my_helper_method
To access helpers from your own controllers, just use:
OrdersController.helpers.order_number(@order)
Source: Stackoverflow.com