How do I defined a variable link_to to an external URL

后端 未结 7 670
名媛妹妹
名媛妹妹 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

提交回复
热议问题