How do I defined a variable link_to to an external URL

后端 未结 7 571
名媛妹妹
名媛妹妹 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:11

    Here's what i did.

    Let's say we have @person and he has a link (@person.link) # => www.google.com

    in your helpers create something like this

    def extlink(link)
    
     if link.include?("http://")
      puts link
     else
      link.insert(0, "http://")
      link
     end
    

    end

    And in your file you can do

    <% @person.each do |p| %>
    
    <%= link_to 'External', extlink(p.link) %>
    
    <% end %>
    

    Works for me

    0 讨论(0)
  • 2020-12-09 08:13

    You can do something like that:

    link_to micropost.website, url_for(micropost.website)
    

    See Rails Api: url_for

    You can experiment in rails console. Just type in console:

    micropost = Micropost.first
    helper.link_to micropost.website, url_for(micropost.website)
    

    And you see a result string.

    Also you need to learn the difference between path and url helpers. See ruby on rails guide.

    Goro rights. You need to add "http://" to your website attribute. After validating and before save Model instance to database you need to add this prefix.

    0 讨论(0)
  • 2020-12-09 08:19

    You can use the ruby URI class

    = link_to micropost.website, URI::HTTP.build({:host => micropost.website}).to_s, target: "_blank"
    
    # <a target="_blank" href="http://www.example.com">www.example.com</a>
    
    0 讨论(0)
  • 2020-12-09 08:20

    I'm working with Rails 5 and I had the same problem. All I need to do to fix it, was to include the protocol on my link_to tag. E.g. I had www.google.com.mx, then, it should be http://www.google.com.mx. And that's it it works just fine like in the official doc is mentioned.

    So, finally I just have something like this in my view:

    <%= link_to (content_tag(:i, "help", class: 'material-icons tiny')), " http://www.google.com.mx", target: "_blank", rel: "alternate" %>
    

    Which is the same as:

    <%= link_to "help", "http://www.google.com.mx", target: "_blank", rel: "alternate" %>
    

    I hope it helps somebody else.

    0 讨论(0)
  • 2020-12-09 08:23

    It sounds like you are storing URLs without the http:// so they are being interpreted as relative URLs. You just need to do something like this:

    link_to micropost.website, "http://#{micropost.website}"
    

    or maybe add a full_url method to that model that adds it if it's missing.

    By the way, you can't use @micropost in that partial because it doesn't exist (you only have @microposts or micropost).

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题