Forum: Ruby on Rails link_to an external url ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
19403f509ca90e8ffe072214c6cba896?d=identicon&s=25 oo00oo (Guest)
on 2006-01-03 13:14
(Received via mailing list)
Simple question. Is it possible to generate a link like

<a href="http://www.google.com" target="_blank" >google</a>

with the link_to function ?

Thanks
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2006-01-03 13:23
(Received via mailing list)
On 3.1.2006, at 14.13, oo00oo wrote:

> Simple question. Is it possible to generate a link like
>
> <a href="http://www.google.com" target="_blank" >google</a>
>
> with the link_to function ?

Yes:

"Itâ??s also possible to pass a string instead of an options hash to
get a link tag that just points without consideration. If nil is
passed as a name, the link itself will become the name." [1]

So, <%= link_to "google", "http://www.google.com" %> should do the job.

//jarkko

[1] http://api.rubyonrails.com/classes/ActionView/Helpers/
UrlHelper.html#M000332
19403f509ca90e8ffe072214c6cba896?d=identicon&s=25 oo00oo (Guest)
on 2006-01-03 13:40
(Received via mailing list)
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
E48d29dc8fedb2878fa518d41cc63d88?d=identicon&s=25 Jan Prill (Guest)
on 2006-01-03 14:00
(Received via mailing list)
Hi, ooOOoo,

as many rails functions the link_to function takes parameters that are
not mandatory for the function as an optional hash. You might write
anything into
this hash. So the answer to your question of a list of possible
:properties would be 'endless'. See for example:

<%= link_to "google", "http://www.google.com" , { :foo => "bar" } %>
producing
<a href="http://www.google.com" foo="bar">google</a>

You might have a look at http://www.w3schools.com/tags/tag_a.asp for key
=> value - pairs that make 'sense'.

Regards
Jan
00e3a96684ab390a350b0271e98741d3?d=identicon&s=25 Nshbrown Nshbrown (nshb)
on 2006-01-03 14:24
(Received via mailing list)
So you know, target="_blank" is not valid XHTML. I recommend reading
about
using the rel="external" tag if you so desire to popup a new window.

Here is some sample JavaScript I have in my default JS library code I
use in
such a case to ensure XHTML compliancy:

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") ==
"external") {
            anchor.target = "_blank";
        }
    }
}
window.onload = externalLinks;

You can find fill documentation for the url_for method at:
http://www.railsmanual.org/class/ActionController%...

In addition, there are 3 special options for the html_options parameter
with
link_to, outlined here:
http://www.railsmanual.org/module/ActionView%3A%3A...
h_link_to

Regards,
Nathaniel

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Nathaniel S. H. Brown                           http://nshb.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00e3a96684ab390a350b0271e98741d3?d=identicon&s=25 Nshbrown Nshbrown (nshb)
on 2006-01-03 14:27
(Received via mailing list)
:target is a HTML (although non XHTML compliant) tag. All other HTML
attributes are valid tags such as :onmouseover, :onclick, :class, :id,
etc
can be used. They are not documented as you should reference W3 for what
HTML tags are truly valid.

-Nb

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Nathaniel S. H. Brown                           http://nshb.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19403f509ca90e8ffe072214c6cba896?d=identicon&s=25 oo00oo (Guest)
on 2006-01-03 16:10
(Received via mailing list)
perhaps :popup => true without sizes options is more simple than js ?
00e3a96684ab390a350b0271e98741d3?d=identicon&s=25 Nshbrown Nshbrown (nshb)
on 2006-01-03 16:45
(Received via mailing list)
Popus tend to lack all the toolbars due to the nature of what a popup
is.
Unless you stack it with options to match the feature set across all
browsers natively you will find that the JavaScript below may be a more
viable option. Search engines do not appreciate popup links and have a
hard
time following them for indexing, so you might be better off using such
a
tactic as mentioned via the JavaScript rel="external" option.

-Nb

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Nathaniel S. H. Brown                           http://nshb.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This topic is locked and can not be replied to.