rails link_to(image_tag)怎么样

时间:2013-11-20 15:24:05

标签: ruby-on-rails ruby-on-rails-4

我得到了这个:

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

如果我这样做:(在customers_path旁边添加“Customer”)

<%= link_to(image_tag("topmenubuttons/kunden_OFF.png" , :mouseover => "topmenubuttons/kunden_OVER.png", :mouseon => "topmenubuttons/kunden_ON.png", :title => "Kunden"), "Customer" ,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 ) %>

但必须有更好的解决方案。

link_to“something”,customer_path,某些东西是链接的名称。

如何在我的示例中将链接的名称传递给rails?

谢谢你们这很有效,但看起来像这样: enter image description here

但我希望客户在最上面,我该怎么做?

2 个答案:

答案 0 :(得分:1)

link_to帮助程序执行的操作是生成打开和关闭<a>标记。第一个参数(在您的情况下为image_tag)是标记之间的内容(这将是链接的名称)。所以你得到:

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

link_to助手的第二个参数是路径,第三个是选项的哈希,它将成为<a>标签的属性。

如果您希望图片和文字都在<a>标记内,则必须将image_tag与文本连接起来。就像你试图做的那样,它在错误的地方有一个逗号。它应该是:

<%= 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 ) %>

我在“客户”周围添加了span,因此可以直接设置样式。

答案 1 :(得分:1)

试试这个:

<%= 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 %>

这将生成一些HTML:

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