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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:38:36+00:00 2026-05-18T07:38:36+00:00

I need to convert a rails 2.3 site so that all external URLs open

  • 0

I need to convert a rails 2.3 site so that all external URLs open in a new window. I could go though every call to link_to and add :target => '_blank', but I’d like to do it in one step for all links, present and future. Is there a way I can monkey patch link_to to get the desired behaviour?

  • 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-18T07:38:37+00:00Added an answer on May 18, 2026 at 7:38 am

    In the end I went with this, in an initialiser:

    module ExternalLinksInNewTabs
      def new_tab_link_to *args, &block
        if block_given?
          options = args.first || {}
          html_options = args[1] || {}
    
          if options.is_a? String
            if ExternalLinksInNewTabs.is_external_link? @controller.request.host, options
              html_options[:target] = '_BLANK'
            end
          end
    
          same_tab_link_to options, html_options, &block
        else
          name = args.first
          options = args[1] || {}
          html_options = args[2] || {}
    
          if options.is_a? String
            if ExternalLinksInNewTabs.is_external_link? @controller.request.host, options
              html_options[:target] = '_BLANK'
            end
          end
    
          same_tab_link_to name, options, html_options
        end
      end
    
      def self.is_external_link? host, url
        host.sub! /^www\./, ''
        url =~ /^http/i && url !~ /^http:\/\/(www\.)?#{host}/i
      end
    end
    
    module ActionView
      module Helpers
        module UrlHelper
          include ExternalLinksInNewTabs
    
          alias_method :same_tab_link_to, :link_to
          alias_method :link_to, :new_tab_link_to
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query that I need to convert to rails like syntax.
I'm new to the Rails framework and my boss tells me that I need
I need to convert keys from type string to hash. The name of all
I have this query in MySQL and I need to convert this to rails
I've a simple Rails app that makes a request to an external website and
I have params[:month,:day,:year] and I need to convert them into a DateTime that I
I'm relatively new to Rails, and I'm building an application that deals with weather
I need to convert values stored in my Rails 3.0 application's database table from
I need to be able to quickly convert an image (inside a rails controller)
I need to upload an image from a rails form using Ajax and convert

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.