Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8228787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:34:47+00:00 2026-06-07T16:34:47+00:00

Im new to Ruby and Rails so forgive me if this an easy question.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:34:49+00:00Added an answer on June 7, 2026 at 4:34 pm
    if my_str =~ %r{\Ahttps?://.+\.(?:jpe?g|png)\z}i
    

    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 text
    • s? — optionally followed by an “s” (to allow https://)
    • :// — the literal text (to prevent something like http-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 either aaa or bbb here, but don’t capture the result
    • jpe?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 .JPG as well as .jpg)

    However, you might be able to get away with just this (more readable) version:

    allowed_extensions = %w[.jpg .jpeg .png]
    if my_str.start_with?('http://') &&
       allowed_extensions.any?{ |ext| my_str.end_with?(ext) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a new ruby/rails user trying to get my mac set up for the
I am a new Ruby on Rails user and had a question. I have
I'm new to Ruby on Rails and I'm sure this question is pretty stupid,
I'm brand new to Ruby/Rails/RSpec, and hopefully this is a dumb/simple question. I'm currently
I'm new to ruby/rails and trying to build a simple Projects / Tags app
I'm new in Ruby/Rails, so, my question: what is the perfect way to upload
I am new to Ruby on Rails and I am trying to make a
I'm new with ruby and rails and have been struggling with this issue for
I'm new to Ruby on Rails and I'm trying to model my table relationships.
I am new to Ruby on Rails and I'm desperately trying to get a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.