How do I defined a variable link_to to an external URL

后端 未结 7 700
名媛妹妹
名媛妹妹 2020-12-09 07:58

On my site a user has a personal profile with a link to his personal external website. The url of the sites I store in a postgresql database under the name website

7条回答
  •  盖世英雄少女心
    2020-12-09 08:27

    I use the postrank-uri gem to normalize the url before passing it to link_to.

    class User < ActiveRecord::Base
      def normalized_webpage
        webpage && PostRank::URI.normalize(webpage).to_s
      end
    end
    

    Then you can use link_to "website", user.normalized_webpage, target: "_blank" in your view. This will for example add the http:// to the url, if it's missing.

提交回复
热议问题