0

My Requirement : To Add validation to make sure that SITEMAP_URL keyword is always included in search_engines.url and it should be of format http://www.example.com/

class SearchEngine < ActiveRecord::Base

  validates :name, :ping_url, :uniqueness => true, :presence => true
  validates :ping_url, :format => {:with => /\=SITEMAP_URL$/}, :presence => true

end

Is the validation i have written is correct .

1 Answer 1

2

The validation looks fine.

Except if you expect /\=SITEMAP_URL$/ to interpolate it won't.

It will only match strings like;

  • "http://some.com?q=SITEMAP_URL"

Is that really what you mean?

If you have

SITEMAP_URL = "/sitemap.xml"

Then you can say

SITEMAP_REGEXP = /\=#{SITEMAP_URL}/

But this will interpret the "." as any character.

So you need to use Regexp.escape

Pretty much

Regexp.new(Regexp.escape("=#{SITEMAP_URL}")+"$")

Should do what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.