Ruby-on-rails link_to 图像标签。如何将类添加到标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2115214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:10:46  来源:igfitidea点击:

link_to image tag. how to add class to a tag

ruby-on-rails

提问by Omnipresent

I am using link_to img tag like following

我正在使用 link_to img 标签,如下所示

<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'), 
:action => 'search', :controller => 'pages'%><span>Search</span></a>

Which results in following html

这导致以下html

<a href="/pages/search"><img alt="Search" border="0" class="dock-item" 
src="/images/Search.png?1264132800" /></a><span>Search</span></a> 

I want the class="dock-item" to go to the <a>tag instead of the img tag.

我希望 class="dock-item" 转到<a>标签而不是 img 标签。

How can i change this?

我怎样才能改变这个?

Update:

更新:

<%= link_to image_tag("Search.png", :border=>0), :action => 'search', 
:controller => 'pages', :class => 'dock-item' %>

results in

结果是

<a href="/pages/search?class=dock-item"><img alt="Search" border="0" 
src="/images/Search.png?1264132800" /></a> 

回答by Staelen

hi you can try doing this

嗨,你可以尝试这样做

link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, {class: 'dock-item'}

or even

甚至

link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, class: 'dock-item'

note that the position of the curly braces is very important, because if you miss them out, rails will assume they form a single hash parameters (read more about this here)

请注意,花括号的位置非常重要,因为如果您错过了它们,rails 将假定它们形成单个散列参数(在此处阅读更多相关信息)

and according to the api for link_to:

并根据link_toapi

link_to(name, options = {}, html_options = nil)
  1. the first parameter is the string to be shown (or it can be an image_tag as well)
  2. the second is the parameter for the url of the link
  3. the last item is the optional parameter for declaring the html tag, e.g. class, onchange, etc.
  1. 第一个参数是要显示的字符串(也可以是 image_tag)
  2. 第二个是链接的url参数
  3. 最后一项是用于声明html标签的可选参数,例如class、onchange等。

hope it helps! =)

希望能帮助到你!=)

回答by Starkers

Just adding that you can pass the link_tomethod a block:

只需添加您可以将link_to方法传递给一个块:

<%= link_to href: 'http://www.example.com/' do %>
    <%= image_tag 'happyface.png', width: 136, height: 67, alt: 'a face that is unnervingly happy'%>
<% end %>

results in:

结果是:

<a href="/?href=http%3A%2F%2Fhttp://www.example.com/k%2F">
    <img alt="a face that is unnervingly happy" height="67" src="/assets/happyface.png" width="136">
</a>

This has been a life saver when the designer has given me complex links with fancy css3 roll-over effects.

当设计师给我提供了带有花哨的 css3 滚动效果的复杂链接时,这是一个救命稻草。

回答by Ravinesh

Best will be:

最好的将是:

link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'

回答by Hyman Daniel

this is my solution:

这是我的解决方案:

<%= link_to root_path do %>
   <%= image_tag "image.jpg", class: "some class here" %>
<% end %>

回答by Toby Hede

Easy:

简单:

<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>

The first param of link_tois the text/html to link (inside the atag). The next set of parameters is the url properties and the link attributes themselves.

link_to的第一个参数是要链接的文本/html(在a标签内)。下一组参数是 url 属性和链接属性本身。

回答by Randy Simon

To respond to your updated question, according to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html...

要回答您更新的问题,根据http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html...

Be careful when using the older argument style, as an extra literal hash is needed:

使用旧的参数样式时要小心,因为需要额外的文字哈希:

  link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
  # => <a href="/articles" class="article" id="news">Articles</a>

Leaving the hash off gives the wrong link:

离开散列会给出错误的链接:

  link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
  # => <a href="/articles/index/news?class=article">WRONG!</a>

回答by vandoona

The whole :action =>, :controller =>bit that I've seen around a lot didn't work for me.

:action =>, :controller =>我在周围看到的整个部分对我不起作用。

Spent hours digging and this method definitely worked for me in a loop.

花了几个小时挖掘,这种方法肯定对我有用。

<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>

Ruby on Rails using link_to with image_tag

Ruby on Rails 使用 link_to 和 image_tag

Also, I'm using Rails 4.

另外,我正在使用 Rails 4。

回答by Daniel Guzmán

I tried this too, and works very well:

我也试过这个,效果很好:

      <%= link_to home_index_path do %>
      <div class='logo-container'>
        <div class='logo'>
          <%= image_tag('bar.ico') %>
        </div>
        <div class='brand' style='font-size: large;'>
          .BAR
        </div>
      </div>
      <% end %>

回答by ksan

You can also try this

你也可以试试这个

<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo"    %></li>

Where "metas-logo" is a css class with a background image

其中“metas-logo”是一个带有背景图片的css类

回答by d1jhoni1b

Hey guys this is a good way of link w/ image and has lot of props in case you want to css attribute for example replace "alt" or "title" etc.....also including a logical restriction (?)

嘿伙计们,这是一种带有图像的链接的好方法,并且有很多道具,以防您想要 css 属性,例如替换“alt”或“title”等.....还包括逻辑限制(?)

<%= link_to image_tag("#{request.ssl? ? @image_domain_secure : @image_domain}/images/linkImage.png", {:alt=>"Alt title", :title=>"Link title"}) , "http://www.site.com"%>

Hope this helps!

希望这可以帮助!