I’m using wysihtml5 in the form of my Post model:
posts/_form.html.erb:
<%= f.text_area :content, id: "wysihtml5-textarea",
placeHolder: "Content" %>
And displaying the results as raw + sanitize:
show.html.erb:
<%= sanitize raw(@post.content), :tags => %w(b i u p br) %>
(I’m only allowing b, i, u, p, and br tags).
The problem with this is that html tags other than those mentioned above (b, i, u, p, and br), are still getting saved in the text area (they are still visible for the user if he clicks edit). I would like to remove then right after the user clicks submit (like how WordPress’ text editor does it).
Any suggestions?
EDIT:
Something I’m trying:
post.rb:
before_save :remove_html_tags
def remove_html_tags
self.content.sanitized_allowed_tags.delete 'div'
end
Check out the API docs: http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
Scroll down a bit, maybe this is what you’re looking for:
UPDATE: If you want to strip tags right before the resource is saved, you could do this on the model level with a
before_savecallback. Or in your controller, in the#createaction right before the resource is saved. If you want to strip certain tags then you could use#strip_tags, for example:but since this is a method from ActiveSupport, you have to include the module in your model: