link_to image_tag external url

281 views
Skip to first unread message

BeagleBen

unread,
Aug 11, 2013, 8:23:45 AM8/11/13
to rubyonra...@googlegroups.com
I am a recent rails convert
 
Is it possible to link to an external site using the  link_to image_tag
 
this is the code I have
 
<div class="twitter s_img"><%= link_to image_tag ("Twitter.png"), https://twitter.com/ %></div>

Dave Aronson

unread,
Aug 11, 2013, 9:41:08 AM8/11/13
to rubyonra...@googlegroups.com
On Sun, Aug 11, 2013 at 8:23 AM, BeagleBen <ste...@stevencahill.co.uk> wrote:

> Is it possible to link to an external site using the link_to image_tag
...
> <%= link_to image_tag ("Twitter.png"), https://twitter.com/ %>

You need to put the URL in quotes. Otherwise Ruby will try to
interpret that as a variable or a method call, and fail.

I would bet you were getting some error message when Rails tried to
render that page. When having a problem, error messages are usually
very helpful to include. What was it in this case? Probably
something like "no such method or local variable", pointing at the
URL, right?

Also, just a bit of extra bonus advice that will help you later: you
*almost* got bitten by a Ruby Gotcha. When you use parentheses on a
method call (like that of image_tag above), avoid putting a space
before the opening paren. This may seem like style nitpicking, but in
fact it is one of those areas where Ruby is unexpectedly
whitespace-sensitive. It didn't matter in this instance because there
was only one argument. However, suppose you call a method with two,
like a trivial one to add two numbers. If you call it as "add 1, 2",
omitting the parens, that's fine. If you call it as "add(1, 2)",
using no spaces around the parens (which seems to be typical Ruby
style), or even "add( 1, 2 )", putting spaces *inside* the parens,
fine. But if you call it as "add (1, 2)", then it thinks you're
trying to pass the add method only *one* parameter instead of the two
it expects. Even if add could handle receiving only one param (like
if it had a default value for the second), that one param would be
"(1, 2)"... which is not a valid expression in Ruby.

For more gotchas, see
https://docs.google.com/presentation/d/1cqdp89_kolr4q1YAQaB-6i5GXip8MHyve8MvQ_1r6_s
(the slides from my Ruby Gotchas presentation).

-Dave

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/.

BeagleBen

unread,
Aug 11, 2013, 10:04:16 AM8/11/13
to rubyonra...@googlegroups.com
Dave
 
Thank you so much for your help, normally work with asp.net mvc, but relly enjoying my return to rails
 
Steven
Reply all
Reply to author
Forward
0 new messages