MKK
MKK

Reputation: 2753

How can I setup validation to replace before it saves?

When a user inputs <h1>John to the text field and saves, I want it to be saved as &gt;h1&lt;John instead.

To make it possible, how can I code in my model?

It's User model's name column.

Upvotes: 1

Views: 27

Answers (1)

basgys
basgys

Reputation: 4400

You just need to create a callback:

class YourModel
  before_save :sanitize_name

  private

    def sanitize_name
      self.name = CGI::escapeHTML(name)
    end
end

Upvotes: 2

Related Questions