Using helpers in model: how do I include helper dependencies?

Ruby on-RailsRubyActiverecordModel

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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)

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

To access helpers from your own controllers, just use:

OrdersController.helpers.order_number(@order)

Solution 5 - Ruby on-Rails

If you want to use a the my_helper_method inside a model, you can write:

ApplicationController.helpers.my_helper_method

Solution 6 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionO. Frabjous-DeyView Question on Stackoverflow
Solution 1 - Ruby on-RailsAlfredddView Answer on Stackoverflow
Solution 2 - Ruby on-RailslorncView Answer on Stackoverflow
Solution 3 - Ruby on-RailsskozzView Answer on Stackoverflow
Solution 4 - Ruby on-RailsTarmoView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAtchyut NagabhairavaView Answer on Stackoverflow
Solution 6 - Ruby on-RailsaxsuulView Answer on Stackoverflow