Simple html link from URL variable call

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?

<strong>Website</strong> <%= link_to @directories.url %></p>

<p>Interested? Contact <%= mail_to @directories.email -%></p> <p><%= @directories.description %></p> <hr />

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

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 <a href> calls: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M000913

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| %>   <strong>Website</strong> <%= link_to directory.url %></p>

<p>Interested? Contact <%= mail_to directory.email -%></p> <p><%= directory.description %></p> <hr /> <% 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

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

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

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/ 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.

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" in that attribute, it will generate <a href="www.domain.com"> 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

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