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

  • SEARCH
  • Home
  • 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 7411989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:28:17+00:00 2026-05-29T06:28:17+00:00

So I have a rails app with a url field for embedding youtube and

  • 0

So I have a rails app with a url field for embedding youtube and vimeo videos. Everything works fine but I cannot find a solution to validating both fields but only when one is used.

A user chooses a video url and has a choice of using vimeo or youtube and upon saving I would like to validate that the url code of the video(youtube or vimeo) is correct.

This is what I have

youtube validate code

validates :url ,
:format => {:with => /(https?):\/\/(www.)?(youtube\.com\/watch\?v=|youtu\.be\/)([A-Za-z0-9_-]*)(\&\S+)?.*/}

Vimeo validate code

validates :url ,
:format => {:with => /http:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/}

When I have the youtube code I cannot save a vimeo video and vice versa.

Both of the above codes work but only one, how can I have it so both work at the same time?

  • 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-05-29T06:28:18+00:00Added an answer on May 29, 2026 at 6:28 am

    You could mash your regexes together into one big incomprehensible regex or you could use a custom validation method:

    validate :youtube_or_vimeo_url
    
    private
    
    def youtube_or_vimeo_url
        return if(self.url =~ /\A(https?):\/\/(www.)?(youtube\.com\/watch\?v=|youtu\.be\/)([A-Za-z0-9_-]*)(\&\S+)?.*/)
        return if(self.url =~ /\Ahttp:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/)
        errors.add(:url, 'Bad video URL etc.')
    end
    

    You could also separate your two YouTube patterns with this approach and get code that is easier to read.

    And while I’m here, if you’re using a lot of slashes in your regexes you might want to use %r{} instead:

    def youtube_or_vimeo_url
        return if(self.url =~ %r{\A(https?)://(www.)?(youtube\.com/watch\?v=|youtu\.be/)([A-Za-z0-9_-]*)(\&\S+)?.*})
        return if(self.url =~ %r{\Ahttp://(www.)?vimeo\.com/([A-Za-z0-9._%-]*)((\?|#)\S+)?})
        errors.add(:url, 'Bad video URL etc.')
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have rails app which are working perfectly in the local computer. But when
I have a rails app that has picked up a bit of traction, but
I have a rails app that is responding to requests at the url '/copy/:collection/:email'.
A lot of my users keep going to http://(rails app URL)/blog , but I
I have a rails app (Rails 3.0) that I need to temporarily take out
I have a rails app that uses the following, rails 3.1, ruby 1.9.2, mysql,
I have a Rails app (Postgres) and I want to add validation based on
I have a rails app with thinking_sphinx successfully working for indexing/searching. I was, however,
I have a Rails app with users, suggestions and searches. There is a many-to-many
I have a rails app that I have serving up XML on an infrequent

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.