-2

I am following a tutorial where we build a Reddit-like application. When you click on Visit URL button, it should get you to the link that the user entered (Example: Name: Stack Overflow Link: https://stackoverflow.com) instead of it going to stack overflow's website, it would be https://i-read-it-kingsong.c9users.io/links/https://www.stackoverflow.com. I am using cloud 9, ruby on rails, and I think there is a problem on my show page. This is what it looks like: enter image description here And then here is the show page: enter image description here This is my show page:

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

<div class="btn-group">
    <%= link_to 'Visit URL', @link.url, class: "btn btn-primary" %>
</div> 

And my index page (Where I show all the links):

<% @links.each do |link| %>
  <div class="link row clearfix">
    <h2>
      <%= link_to link.title, link %><br>
      <small class="author">Submitted <%= time_ago_in_words(link.created_at) %> by <%= link.user.email %></small>
    </h2>
  </div>
<% end %> 

Thanks for the help!

iiRosie1
  • 162
  • 2
  • 17
  • Can you please explain in more details? I am not getting you question. – sam Jan 04 '18 at 08:47
  • So this is a website where you post a link, and give the link a title. So the form is like Name: Stack Overflow Link: https://stackoverflow.com. When you click on visit URL, it's supposed to link you to https://stackoverflow.com, but instead, it's the link to my website and then /https://stackoverflow.com. So the problem is that it just adds the website as a route – iiRosie1 Jan 04 '18 at 16:12
  • @sam I added a picture. – iiRosie1 Jan 04 '18 at 16:14

1 Answers1

0

My initial thought is to check what's in the database. Confirm it's the link you're wanting.

Some thoughts on your code:

What is this line? <a href="<%= @link.url %>"><%= @link.title %></a> You're putting 2 things in the same tag - might be the culprit. You should also stick to link_to if that's what you're using for links on the rest of the project.

Additionally - in your loop should probably look more like this (notice the url on 2nd line):

<% @links.each do |link| %>
   <%= link_to link.title, link.url %>
<% end %>

Hope one of these things helps.

EDIT Change the tag line to: <%= link_to @link.title, @link.url %>

gwalshington
  • 1,418
  • 2
  • 30
  • 60
  • The line `<%= @link.title %>` Is the href is @link.url and then it should show the title of the link (link.title) for the a tags. – iiRosie1 Jan 04 '18 at 00:06
  • @iiRosie1 change that line to `<%= link_to @link.title, @link.url %>` – gwalshington Jan 04 '18 at 03:52
  • But it stays the same. – iiRosie1 Jan 04 '18 at 16:10
  • Please be more descriptive. It's difficult to understand what you're wanting. What is 'it' and what is 'same' – gwalshington Jan 04 '18 at 16:14
  • Your new edit completely changes the question. Please be as descriptive as possible from the beginning. This is what you're looking for: https://stackoverflow.com/questions/16721816/how-do-i-defined-a-variable-link-to-to-an-external-url – gwalshington Jan 04 '18 at 16:15
  • Sorry for not being descriptive. It works now. Just I think for the link that I create before I made those changes (you suggested) doesn't work. I think I just need to re-create it for it to work. Thanks a lot! – iiRosie1 Jan 04 '18 at 16:25