views:

344

answers:

3

I am using link_to img tag like following

<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'), 
:action => 'search', :controller => 'pages'%><span>Search</span></a>

Which results in following html

<a href="/pages/search"><img alt="Search" border="0" class="dock-item" 
src="/images/Search.png?1264132800" /></a><span>Search</span></a> 

I want the class="dock-item" to go to the <a> tag instead of the img tag.

How can i change this?

Update:

<%= link_to image_tag("Search.png", :border=>0), :action => 'search', 
:controller => 'pages', :class => 'dock-item' %>

results in

<a href="/pages/search?class=dock-item"><img alt="Search" border="0" 
src="/images/Search.png?1264132800" /></a> 
A: 

Easy:

<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>

The first param of link_to is the text/html to link (inside the a tag). The next set of parameters is the url properties and the link attributes themselves.

Toby Hede
I had tried that but adding it there makes it a parameter for the string. please see my update
Omnipresent
Yeah, I even wrote that the second param is the url, but then deleted it for no reason :P
Toby Hede
+1  A: 

hi you can try doing this

link_to image_tag("Search.png", :border => 0), {:action => 'search', :controller => 'pages'}, {:class => 'dock-item'}

or even

link_to image_tag("Search.png", :border => 0), {:action => 'search', :controller => 'pages'}, :class => 'dock-item'

note that the position of the curly braces is very important, because if you miss them out, rails will assume they form a single hash parameters (read more about this here)

and according to the api for link_to:

link_to(name, options = {}, html_options = nil)
  1. the first parameter is the string to be shown (or it can be an image_tag as well)
  2. the second is the parameter for the url of the link
  3. the last item is the optional parameter for declaring the html tag, e.g. class, onchange, etc.

hope it helps! =)

Staelen
Thanks for the detailed answer. I'll accept your answer but let me know if you know how to achieve this with link_to tag as well..`<a href="/pages/search" class="dock-item"><img alt="Search" border="0" src="/images/Search.png?1264132800" /><span>Search</span></a>` I've added a span tag before closing `<a>` tag
Omnipresent
it's actually the same concept, so you need to find ways to squeeze both the image_tag and the span into parameter "name". you can try this, not the full code but i think you can do it yourself =) =link_to "#{image_tag}<span>Search</span>", ... can you see what i'm trying to do?
Staelen
ah got it. yeah I wanted to squeez it in name parameter. but was not aware of #{}. fun times with rails
Omnipresent
A: 

To respond to your updated question, according to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html...

Be careful when using the older argument style, as an extra literal hash is needed:

  link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
  # => <a href="/articles" class="article" id="news">Articles</a>

Leaving the hash off gives the wrong link:

  link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
  # => <a href="/articles/index/news?class=article">WRONG!</a>
Randy Simon