link_to image_tag external url

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

<%= link_to image_tag ("Twitter.png"), []( %>

Is it possible to link to an external site using the link_to image_tag


<%= link_to image_tag ("Twitter.png"), %>

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

(the slides from my Ruby Gotchas presentation).



Thank you so much for your help, normally work with mvc, but relly enjoying my return to rails