简体   繁体   中英

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: 在此输入图像描述

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM