Ruby on Rails Sunday, August 11, 2013

On Sun, Aug 11, 2013 at 8:23 AM, BeagleBen <steven@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/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQihKMKA9mrK3vvPPMAW2AmpvPeOdYHcrUo2Pb0MHeHWqbg%40mail.gmail.com?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment