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 8195119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:01:36+00:00 2026-06-07T05:01:36+00:00

This question has answered the question for a single column but how do you

  • 0

This question has answered the question for a single column but how do you do it for multiple columns? I’ve got three columns (website, fb, twitter) that I want to prefix with http in case users don’t input them in the form.

I tried this but it doesn’t work:

before_save :sanitize_links

private

def sanitize_links
  website = self.website
  facebook = self.facebook
  twitter = self.twitter
  links = [website, facebook, twitter]
  links.each do |link|
    unless link.include?("http://") || link.include?("https://")
      link = "http://" + link
    end
  end
end

Update

I’ve tried the suggestion by KL-7 but unfortunately hit a little snag. How do I use the output of the array with before_save? I’ve tried the code below but it doesn’t work.

before_save :sanitize_links

private

def sanitize_links
  links = ["website", "facebook", "twitter"]
  links.map! { |link| self.link =~ %r{\Ahttps?://} ? self.link : "http://" + self.link }
end

Update 2

I gave up. I’ll just repeat it three times:

before_save :sanitize_links

private

def sanitize_links # prefix user-submitted links with http:// if missing
  self.website =~ %r{\Ahttps?://} ? self.website : self.website = "http://" + self.website
  self.facebook =~ %r{\Ahttps?://} ? self.facebook : self.facebook = "http://" + self.facebook
  self.twitter =~ %r{\Ahttps?://} ? self.twitter : self.twitter = "http://" + self.twitter
end
  • 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-07T05:01:37+00:00Added an answer on June 7, 2026 at 5:01 am

    The problem is that

    link = "http://" + link
    

    in your code assigns a new value to the link variable withing the block, but doesn’t change the corresponding element of the list.

    To alter elements of the list in-place you can use Array#map!:

    links.map! do |link|
      link.include?("http://") || link.include?("https://") ? link : "http://" + link
    end
    

    Another thing I’m a bit worried about is that include? will return true even if the given substring (e.g., 'http://' occurs somewhere in the middle of your string and not necessarily at the beginning. I’d rather use a regexp for that task:

    links.map! { |link| link =~ %r{\Ahttps?://} ? link : "http://" + link }
    

    Update

    If link is an attribute of your model, then you should at least store it as an attribute and not as a local links variable:

    self.links = links.map { |link| link =~ %r{\Ahttps?://} ? link : "http://" + link }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been already asked, but never answered. I want to write some
I realize that this question has been asked and has been answered here but
Now, I realize that this question has been answered before, but I just can't
I am sure this question has been answered somewhere but I'm having major problems
sorry if this question has already been answered somewhere else, but I couldn't find
I know this type of question has been asked and answered before but I
This question has been answered. The problem was that myEventMoveTrainManaul() was being called from
Maybe this question has been answered before, but the word if occurs so often
I'm sure this question has been answered before, but I can't find an answer
May be this question has been answered before but I couldn't find it. I

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.