4

我的初衷是在图像上显示一些文字。同时,当我们点击图片时,网页会被重定向。我将 link_to 函数与包含背景图像的 div 一起使用。代码是这样的:

 <%= link_to raw('<div style="background-image:url(<%= image_url  '1.jpg'%>);width:340px;"> This is a  test</div>'),index_by_dvp_domain_path %>

但是系统告诉我有 SyntaxError。

4

5 回答 5

4

您可以将 link_ 传递给包含您要显示的内容的块。因此,与其一起去,不如去你能做的link_to(display, url, options={})地方link_to(url, option={}, &block)

<%= link_to index_by_dvp_domain_path do %>
  <div style="background-image: url(<%= image_url '1.jpg'%>);width:340px;"> 
   This is a  test
  </div>
<% end %>

完成此操作后,您可以将其视为普通 html。

与往常一样,我建议尝试将任何样式移到它自己的单独样式表中。

于 2013-08-06T09:53:21.567 回答
1

最好的方法是使用以下方法

<%= link_to index_by_dvp_domain_path do
      content_tag(:div, 'This is a  test',:style=>"background-image:url(#{image_url}  '1.jpg');width:340px;" )
    end
%>

或者

<%= link_to content_tag(:div, 'This is a  test',:style=>"background-image:url(#{image_url}  '1.jpg');width:340px;" ), index_by_dvp_domain_path  %>
于 2013-08-06T10:03:54.800 回答
0

请尝试一下

<%= link_to raw('<div style="background-image:url(#{image_url  '1.jpg'}%>);width:340px;"> This is a  test</div>'),index_by_dvp_domain_path %>
于 2013-08-06T09:57:46.220 回答
0

我认为即使您有一个包含多个标签的大块,使用下面的 Link_to 也会更简单:

<%= link_to desired_path do %>
    <div class="linkable">
        <another div>
             ... some other tags
        </another div>
    </div>
<% end %>

我建议您为鼠标悬停事件使用不同的背景颜色,因为它向查看者显示它是一个链接!

在你的 .css 文件中:

.linkable:hover{
    background-color: red;
}
于 2016-04-15T06:17:55.123 回答
-2

我很惊讶没有人想出在 Rails 中做这件事的常规方法。

<%= link_to image_tag("/images/1.jpg",:size => "340X340"),index_by_dvp_domain_path %>
于 2013-08-06T10:48:54.777 回答