如何将图像转换为Rails中的链接?

Spe*_*ley 42 ruby-on-rails

我只想在图像上做一个简单的链接.

<a href="http://www.mysite.com"><img src="path/to/image.png"/></a>
Run Code Online (Sandbox Code Playgroud)

你如何使用link_to rails标签做到这一点?

Mic*_*ley 68

使用一个image_tag用于的内容link_to.

link_to image_tag("path/to/image.png"), "http://www.mysite.com/"
Run Code Online (Sandbox Code Playgroud)

  • 没关系,我明白了.link_to image_tag("path/to/image.png",:class =>"class_name"),"http://www.google.com/" (7认同)

Jac*_*iel 24

我的解决方案

<%= link_to root_path do %>
   <%= image_tag "image.jpg", class: "some css class here" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)


Pra*_*vin 11

干一个
在你的application_helper.rb

def link_to_image(image_path, target_link,options={})
  link_to(image_tag(image_path, :border => "0"), target_link, options)
end
Run Code Online (Sandbox Code Playgroud)

然后从你的意见

<%= link_to_image("path/to/image", some_url) %>
Run Code Online (Sandbox Code Playgroud)


Mik*_*wis 6

<%= link_to(image_tag("path/to/image.png"), root_path) %>
Run Code Online (Sandbox Code Playgroud)

root_path您网站主页的路线在哪里.