Reputation: 6815
How do I make Rails treat the "http://" value as blank: if the value is "http://", do not validate website field and insert an empty string (not "http://") into database?
In view:
<%= f.text_field :website, value: "http://" %>
In model:
validates :website, format: { with: /^https?:\/\/\S+/i }, allow_blank: true
Upvotes: 0
Views: 579
Reputation: 7127
You can use :if or :unless to conditionally validate (untested):
validates :website, format: { with: /^https?:\/\/\S+/i }, allow_blank: true, :unless => ['http://', 'https://'].include?(params[:website]) }
Use a before_save callback to convert the string to blank:
def before_save
self.website = "" if ['http://', 'https://'].include?(self.website)
true
end
Upvotes: 1