3

I've a problem in my program, I add in DB a link, for example, "www.google.com" and when I clic in the link I'm redirected to localhost:3000/www.google.com, this doesn't happen when I put "http://www.google.com" in DB.

My code

<td><%= link_to t.title, t.link_to_idea, :target => "_blank" %></td>

How do I make to convert this link always in absolute? (I think I this's solution)

Thanks!!

4
  • 2
    maybe simply update your database and add "http://" where needed? Sep 6, 2017 at 15:10
  • 1
    I suggest doing what @SergioTulentsev recommended. stackoverflow.com/questions/7908598/…
    – AbM
    Sep 6, 2017 at 15:12
  • But I don't permition to modific the DB, only if I modific my code to always, in moment of save to link, put the http or https. But it works only one options Sep 6, 2017 at 16:04
  • If your code has write access to the DB, you can always write a script that updates it for you. If you go this route, make sure you validate future links are in the right format. Sep 6, 2017 at 19:16

3 Answers 3

0

You could do something like:

<td><%= link_to t.title, t.link_to_idea.start_with?('http') ? t.link_to_idea : "http://#{t.link_to_idea}", :target => "_blank" %></td>

..but that assumes you want all links to save with http and not https. You're probably better off checking for the protocol before you save the link in the DB.

For example, you could do what this answer suggests: Add http(s) to URL if it's not there?

before_validation :smart_add_url_protocol

protected

def smart_add_url_protocol
  unless self.url[/\Ahttp:\/\//] || self.url[/\Ahttps:\/\//]
    self.url = "http://#{self.url}"
  end
end

That way you can just do what you already have.

2
  • I think your solution is the best until this moment, but how do you say, I have to choose one option, http or https. I would like to directed that's for what is in the DB column Sep 6, 2017 at 16:10
  • My friend, your sugestion it worked. Now, whan I put the mouse over link, show me the link "www.google.com" and no more "localhost:3000/www.google.com" Thank you very much!!! Sep 6, 2017 at 16:57
0

I think your best bet is to update the links in your database to all conform to a standard format. You can also add a more basic validation to make sure all links match a valid format:

validates :link_to_idea, format: URI.regexp

You can also run a backfill on your database that checks old links to make sure they match this pattern and then update the ones that do not work. Are you using MySQL?

Either way, the best answer is not to try to make your app render any-old-thing that the user puts in, but to clean the data before it gets into the database.

If you can't control what goes into the database, then I would simply render, as text, anything that doesn't match that Regexp and let users put that into their browser on their own.

2
  • So, I use Postgres, but I don't have acess to modify to DB Sep 6, 2017 at 16:15
  • I think that's fine, simple add the validation (so you don't get more dirty data, and check the urls that you output to the page (via the Regex) and if they don't match, then don't make them a link.
    – apanzerj
    Sep 6, 2017 at 20:52
0

I'd suggest that you create a decorator using Draper. This will allow you to decouple presentation logic from your domain object.

Once you've set it up then you could write something similar to this:

# app/decorators/idea_decorator.rb
class IdeaDecorator < Draper::Decorator
  delegate_all   

  def idea_url(protocol = 'https')
    return link_to_idea if has_scheme?

    "#{protocol}://#{link_to_idea}"
  end

  private

  def has_scheme?
    # .. some method here to determine if the URL has a protocol
  end
end

And used in the view:

<%= link_to t.title, t.decorate.idea_url('https') %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.