Ruby on rails Rails:将_链接到图像标签。如何将类添加到标记中
我正在使用链接到img标签,如下所示Ruby on rails Rails:将_链接到图像标签。如何将类添加到标记中,ruby-on-rails,Ruby On Rails,我正在使用链接到img标签,如下所示 <%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'), :action => 'search', :controller => 'pages'%><span>Search</span></a> 0,:class=>dock item'), :action=>'search',:controller
<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'),
:action => 'search', :controller => 'pages'%><span>Search</span></a>
0,:class=>dock item'),
:action=>'search',:controller=>'页面'%>搜索
这将导致以下html
<a href="/pages/search"><img alt="Search" border="0" class="dock-item"
src="/images/Search.png?1264132800" /></a><span>Search</span></a>
搜索
我希望class=“dock item”转到
简单:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>
0),:action=>'search',:controller=>'pages',:class=>'dock item'>
link_to的第一个参数是要链接的文本/html(在a标记内)。下一组参数是url属性和链接属性本身。您好,您可以尝试这样做
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, {class: 'dock-item'}
甚至
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, class: 'dock-item'
请注意,大括号的位置非常重要,因为如果错过了大括号,rails将假定它们形成一个散列参数(请阅读更多相关信息)
并根据:
希望有帮助!=) 根据 使用较旧的参数样式时要小心,因为需要额外的文本哈希:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => <a href="/articles" class="article" id="news">Articles</a>
link_到“Articles”,{:controller=>“Articles”},:id=>“news”,:class=>“article”
# =>
不使用散列会产生错误的链接:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => <a href="/articles/index/news?class=article">WRONG!</a>
链接到“错误!”,:controller=>“articles”,:id=>“news”,:class=>“article”
# =>
最好是:
link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'
嘿,伙计们,这是一个链接w/image的好方法,并且有很多道具,以防你想要css属性,例如替换“alt”或“title”等…还包括一个逻辑限制(?)
“Alt title”,:title=>“Link title”})http://www.site.com"%>
希望这有帮助 只需添加您可以将
链接\u传递给方法a块:
<%= link_to href: 'http://www.example.com/' do %>
<%= image_tag 'happyface.png', width: 136, height: 67, alt: 'a face that is unnervingly happy'%>
<% end %>
结果:
当设计师给了我复杂的链接和奇特的css3滚动效果时,这是一个救命稻草。'Vivek',:title=>'Vivek',:class=>'dock-item')%>
<%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>
我经常看到的整个:action=>,:controller=>
对我来说不起作用
花了好几个小时挖掘,这种方法在一个循环中确实对我有效
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
“工作”)%%>
另外,我使用的是Rails 4 这是我的解决方案:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some class here" %>
<% end %>
您也可以试试这个
<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo" %></li>
其中“metas logo”是一个带有背景图像的css类我也尝试过,效果非常好:
<%= 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 %>
.酒吧
我已经尝试过了,但是在那里添加它会使它成为字符串的参数。请看我的updateYeah,我甚至写道第二个参数是url,但随后又无缘无故地删除了它:感谢详细的答案。我会接受你的回答,但如果你知道如何通过link_to tag实现这一点,请告诉我
在关闭
标记之前,我添加了一个span标记。它实际上是同一个概念,因此您需要找到方法将image_标记和span压缩到参数“name”中。你可以试试这个,不是完整的代码,但我认为你可以自己做=)=链接到“#{image_tag}Search”。。。你能看到我在做什么吗?啊,明白了。是的,我想在name参数中压缩它。但不知道#{}。与Rails的有趣时光这可能对其他人有帮助:如果你只是想在链接文本旁边添加一个图像,你只需这样做,我不需要链接之前的额外内容。如果你去掉href,你可以直接转到url。这对我来说是最好的答案。谢谢@NewUserName此处无需担心!:)请您解释一下我们答案的细节好吗?我的ans将显示如下输出:SearchThis是最干净的解决方案。最好的解决方案,干净且没有任何不必要的循环。
<%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some class here" %>
<% end %>
<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo" %></li>
<%= 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 %>