# Simple html link from URL variable call

**URL:** <https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836>\
**Category:** Rails\
**Created:** [April 25, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836 "2008-04-25T14:07:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 25, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/1 "2008-04-25T14:07:23Z")

</div>

I am building a business listing page, and there is a field for URL  
and email. I have the email link working with mail\_to, but is there  
also a method call for an URL?

**Website** \<%= link\_to @directories.url %\>

Interested? Contact \<%= mail\_to @directories.email -%\>

\<%= @directories.description %\>

 
* * *

It does link, but not to the implied website. Is my mistake using  
“url”, rather than “www” or something else?

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 25, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/2 "2008-04-25T14:55:22Z")

</div>

Um… what is @directories? An array of Directory objects?

@directories.url doesn’t make much sense.

But, link\_to is the right “tool” to use for building calls:

> **[ActionView::Helpers::UrlHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M000913)**
>
> Provides a set of methods for making links and getting URLs that depend on the routing subsystem (see ActionDispatch::Routing).

If, I’m assuming, @directories is an array of Directory objects, and a  
Directory has a url field and an email field, then you could do:

\<% @directories.each do |directory| %\>  
**Website** \<%= link\_to directory.url %\>

Interested? Contact \<%= mail\_to directory.email -%\>

\<%= directory.description %\>

 
* * *
\<% end %\>

On the other hand, if @directories is just an object that has the  
three fields (url, email, description), then your original code is  
correct and I’d suspect the format or content of the url field. Oh,  
and if @directories is just an object, you might want to rethink  
things. It is usually good (er… very good) practice to use singular  
naming for a single object and plural naming for a collection of  
objects.

HTH!

-Danimal

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 25, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/3 "2008-04-25T17:51:01Z")

</div>

Directory is a table in my DB that collects the input from the user.  
The “url” is one of the fields from this table. In the show page, I  
want the url to show up and be “clickable” just like the email. I  
tried the \<% @directories.each do |directory| %\> , but received a  
compile error (unexpected $end).  
I then tried putting an \<% end %\>, but that made the website field  
disappear.  
My Directory table field is set up as a “string”, if that helps.  
I tried different combinations of plural and not.

-Kyle

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 25, 2008, 9:13pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/4 "2008-04-25T21:13:33Z")

</div>

Take a closer look at link\_to. The first parameter that you pass is  
the text that you want to have displayed, and the second is the URL.  
If you want the url text and target to be the same, do:

\<%= link\_to @directories.url, @directories.url %\>

-Kyle

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 26, 2008, 8:55am UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/5 "2008-04-26T08:55:55Z")

</div>

OK,

This is looking better, and I am close to a solution here. The link  
now has the web address in it, but includes “[http://localhost:3000/](http://localhost:3000/)  
directories/www.mywebsite.com”.  
By the way, thanks everyone for chiming in here!

-Kyle

p.s. Kyle, I needed a double take when I saw my name on the last  
answer… I though I had become very smart over night and started  
helping people here. Perhaps one day when I know WTF I am doing in  
RoR.

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 28, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/6 "2008-04-28T14:33:34Z")

</div>

Yeahhhh! That was the missing part! A thousand thanks Kyle!! I knew  
that it could not be that hard to make a simple HTML link work. I  
would also like to thank Danimal for looking into this as well.

Best regards,

Kyle

---

<div class="post-metadata">

**Author:** ![Laughlink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d78d45/32.png) [@Laughlink](https://www.ruby-forum.com/u/Laughlink)\
**Post date:** [April 27, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/simple-html-link-from-url-variable-call/135836/7 "2008-04-27T16:05:18Z")

</div>

Made me look twice, too!

Do you have a full URL (i.e., with the “http://” and domain and path)  
in your @directories.url attribute? If you only have “[www.domain.com](http://www.domain.com)”  
in that attribute, it will generate which  
ends up being relative because it doesn’t specify “http://”. You can  
either:

 
1. change your @directories listings to include the protocol, or
2. generate the “http://” part within the link\_to helper.

 

There are probably other options. To do the latter, use:

 

\<%= link\_to @directories.url, (“http://” + @directories.url) %\>

 

…or something similar. You might not need those parentheses.

 

I think that’s what’s happening, anyway. If not, please give an  
example record for a directory in your DB.

 

-Kyle
