0

I have a rails app where users can post links inside a pin model. Here is the code to explain:

app/views/pins/_form.html.erb:

 <%= f.text_field :website, placeholder: "http://wwww.website.com" %>

And in my views:

<% if @pin.website.blank? %>
<% else %>
  <a href="<%= @pin.website %>" target="_blank">
<% end %>

It is working well if the users put the complete url (http://www.website.com) in the field, but if he forgets to add the 'http://' => (www.website.com) the page is obviously broken.

I was thinking to use the gem auto_link but I am a little bit confused on how to use it. Here is my config:

gemfile.rb

gem 'rails_autolink', '~> 1.1', '>= 1.1.6'

views:

<% if @pin.website.blank? %>
<% else %>
  <%= auto_link(@pin.website) %>
<% end %>

application.rb

require 'rails_autolink'

But it just doesn't work, the link appears as plain text. Any help, what am I missing. I have been looking on other thread, but nothing seems to resolve my issue...

UPDATE: It is working well now if user passes the url just with the www. and without the http://, but is there a way to transform a raw text like this: website.com to http://www.website.com?

DaBler
  • 2,695
  • 2
  • 26
  • 46

1 Answers1

1

auto-link still needs the prepended HTTP or HTTPS on the web address. My suggestion is going from the controller layer and parse it from there. If the submitted website does not have "http" in it, then add it.

pauloancheta
  • 349
  • 2
  • 9