简体   繁体   中英

How do I validate TinyMCE length in Ruby on Rails?

I need to validate the length of short_description field, which is filled in using TinyMCE. The thing is, that I must strip string of HTML tags first. Tried couple of solutions with no luck. Hope that somebody will be able to help me.

This is what I've got so far:

validates_length_of :description_short, :maximum => 300, :tokenizer => lambda { |string| strip_tags(string).split(//) }, :message => 'too long'

It raises error undefined method `strip_tags' for #

To use strip_tags in the model you need to specify where it is at the moment of calling it:

ActionController::Base.helpers.strip_tags()

Resulting the given code in:

validates_length_of :description_short, :maximum => 300, 
    :tokenizer => lambda { |string| ActionController::Base.helpers.strip_tags(string).split(//) }, 
    :message => 'too long'

Some people suggest to include ActionController at the top of the model, but it's not considered a best practice because loads all the Helpers into de model, slowing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM