Im new to Ruby and Rails so forgive me if this an easy question. Im trying to check when a user passes in an IMG url in my form, that it is a valid url. Here is my code:
if params[:url].include? 'http://' && (params[:url].include? '.jpg' || params[:url].include? '.png')
This returns and error. Is this is even the best way to go about it? What should I do differently? Thanks.
Regex explained:
%r{...}— regex literal similar to/.../, but allows/to be used inside without escaping\A— the start of the string (^is just the start of the line)http— the literal texts?— optionally followed by an “s” (to allow https://)://— the literal text (to prevent something likehttp-whee.jpg).+— one or more characters (that aren’t a newline)\.— a literal period (make sure this is an extension we’re looking at)(?:aaa|bbb)— allow eitheraaaorbbbhere, but don’t capture the resultjpe?g— either “jpg” or “jpeg”png— the literal text\z— the end of the string ($is just the end of the line)i— make the match case-insensitive (allow for.JPGas well as.jpg)However, you might be able to get away with just this (more readable) version: