1

(new to Stack Overflow, please be gentle)

In my Rails app, which is much like Reddit, I'd like a user to be able to click on the link posted and... actually go to that link. (They'd be going to other websites, as you'd expect.) For instance, if someone posted a title with a link going to www.google.com, someone should be able to click on the title text and, you know, go to Google.

But the error that always shows up when clicking a link is:

Routing Error

No route matches [GET] "/links/www.google.com"

(EDIT) Here is my relevant part of the Show page:

<div class="page-header">
<h1><a href="<%= @link.url %>"><%= @link.title %></a><br>            
<small>Submitted by <%= @link.user.email %></small></h1>
</div>

<%= image_tag @link.image.url(:medium) %>
<br>
<br>
<div class="btn-group">
<%= link_to 'Visit URL', @link.url, class: "btn btn-primary" %>
</div>

<% if @link.user == current_user %>
<div class="btn-group">
    <%= link_to 'Edit', edit_link_path(@link), class: "btn btn-default" %>
    <%= link_to 'Destroy', @link, method: :delete, data: { confirm:  'Are you sure?' }, class: "btn btn-default" %>
</div>
<% end %>`

Does anyone know why that might be? And how to fix the issue?

Community
  • 1
  • 1
  • Possible duplicate of [Creating a rails route to an external URL](http://stackoverflow.com/questions/3622706/creating-a-rails-route-to-an-external-url) – jacefarm Nov 02 '16 at 20:40

1 Answers1

3

It is hard for me to see the exact problem without you posting a little more information like the view for the page you are talking about, but in rails it assumes most links are relative so you are going to want it to be have the protocol on the link i.e. 'http://' or 'https://', so something like

<%= link_to 'Title', 'http://www.url.com' %>.

How you go about implementing that in your application really depends and I'd have to have more code to figure out but I hope this helped.

  • Thank you! I added the relevant code (I believe) of the Show page. I really appreciate the help. I'm incredibly new at this. – Aurelius180 Nov 03 '16 at 15:11
  • It doesn't yet work, however. What I meant to say was that I'd like the user to click on a link entered by the person who created the post. So, Person A posts "Google" and when Person B clicks on it, they go to http://www.google.com ... similarly, had Person A posted the same sort of thing but directed it to Yahoo!, the person clicking it would go to http://www.yahoo.com. ... The link would go wherever Person A specifies it. Right now, it just throws up the error. – Aurelius180 Nov 03 '16 at 16:08
  • I was able to recreate your problem in an app I threw together. If you want it to go to the `@link.url` so if the person says the link is 'google.com' you still want it to go to 'http://google.com' you can either add a before_save method on the model and actually change the text they enter to have http in the begining or create a helper method to use the "http://"url even if it isn't in the `@link.url`. I created a gist [here](https://gist.github.com/Schenktar/b23b0114df56075bf246c2bda1bb1ee1) to show it. Let me know if that doesn't work for you – Jonathan Schenk Nov 03 '16 at 18:13
  • It worked using string interpolation! Like so:
    <%= link_to 'Visit URL', "https://#{@link.url}", class: "btn btn-primary" %>
    Thanks for your help and advice!
    – Aurelius180 Nov 04 '16 at 20:58