Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use strip_tags and truncate inside my Model.rb?

strip_tags works for me in my view page, but not in my model (i'm using it in a before_save function)

like image 880
Blankman Avatar asked Feb 17 '11 05:02

Blankman


2 Answers

You can also access helpers in your models like this:

ActionController::Base.helpers.strip_tags(text)
like image 117
Aleks Avatar answered Oct 16 '22 05:10

Aleks


ActionView::Base.full_sanitizer.sanitize(your_html_string)

This will work for you. Or you can define a helper like:

def strip_html_tags(string)
    ActionView::Base.full_sanitizer.sanitize(string)
end

And then use this like:

strip_html_tags(your_html_string)

To remove some specific tags pass additional parameters. Source: Sanitize Rails API Dock

like image 43
Malik Shahzad Avatar answered Oct 16 '22 04:10

Malik Shahzad