link_to image tag. how to add class to a tag

前端 未结 11 1771
失恋的感觉
失恋的感觉 2020-12-22 20:53

I am using link_to img tag like following

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


        
相关标签:
11条回答
  • 2020-12-22 21:04

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

    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

    Also, I'm using Rails 4.

    0 讨论(0)
  • 2020-12-22 21:06

    Just adding that you can pass the link_to method a block:

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

    0 讨论(0)
  • 2020-12-22 21:07

    To respond to your updated question, according to 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>
    
    0 讨论(0)
  • 2020-12-22 21:08

    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

    0 讨论(0)
  • 2020-12-22 21:14

    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 (?)

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

    0 讨论(0)
  • 2020-12-22 21:17

    this is my solution:

    <%= link_to root_path do %>
       <%= image_tag "image.jpg", class: "some class here" %>
    <% end %>
    
    0 讨论(0)
提交回复
热议问题