user3008711
user3008711

Reputation: 33

rails link_to(image_tag) how to

I got this one:

<%=link_to(image_tag("topmenubuttons/kunden_OFF.png",:mouseover => "topmenubuttons/kunden_OVER.png", :mouseon =>"topmenubuttons/kunden_ON.png", :title => "Kunden"), customers_path ) %>

if i do this: (appended "Customer" next to customers_path)

<%= link_to(image_tag("topmenubuttons/kunden_OFF.png" , :mouseover => "topmenubuttons/kunden_OVER.png", :mouseon => "topmenubuttons/kunden_ON.png", :title => "Kunden"), "Customer" ,customers_path ) %>

I get an error:

I could do this:

<%= link_to(image_tag("topmenubuttons/kunden_OFF.png" , :mouseover => "topmenubuttons/kunden_OVER.png", :mouseon => "topmenubuttons/kunden_ON.png", :title => "Kunden"), +"Customer" ,customers_path ) %>

But there must be a better solution.

link_to "something", customer_path, something is the name of the link.

How can I pass the name of the link to rails in my example?

Thank you guys It works but it looks like this: enter image description here

But I'd like the Customer on top, how can I do this?

Upvotes: 0

Views: 187

Answers (2)

Thanh
Thanh

Reputation: 8624

Let try this:

<%= link_to customers_path do %>
  <%= image_tag("topmenubuttons/kunden_OFF.png" , :mouseover => "topmenubuttons/kunden_OVER.png", :mouseon => "topmenubuttons/kunden_ON.png", :title => "Kunden") %>
  <span>Customer</span>
<% end %>

This will generate some HTML like:

<a href="/customers">
  <img ...>
  <span>Customer</span>
</a>

Upvotes: 1

DiegoSalazar
DiegoSalazar

Reputation: 13531

What the link_to helper does is generate the opening and closing <a> tags. The first argument (in your case the image_tag) is what goes in between the tags (this would be the name of the link). So you'd get:

<a href="/customers">
  <img src="topmenubuttons/kunden_OFF.png" mouseover and stuff... />
</a>

The second argument to the link_to helper is the path, the 3rd is a hash of options that will become the attributes of the <a> tag.

If you want both the image and the text to be within the <a> tag you do have to concatenate the image_tag with the text. Like you attempted to do but, it had a comma in the wrong place. It should be:

<%= link_to(image_tag("topmenubuttons/kunden_OFF.png" , :mouseover => "topmenubuttons/kunden_OVER.png", :mouseon => "topmenubuttons/kunden_ON.png", :title => "Kunden") + "<span>Customer</span>" , customers_path ) %>

I included a span around "Customer" so it can be directly styled.

Upvotes: 1

Related Questions