link_to image tag. how to add class to a tag

前端 未结 11 1792
失恋的感觉
失恋的感觉 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:18

    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)

    and according to the api for link_to:

    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.

    hope it helps! =)

    0 讨论(0)
  • 2020-12-22 21:20
    <%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>
    
    0 讨论(0)
  • 2020-12-22 21:21

    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 %>
    
    0 讨论(0)
  • 2020-12-22 21:22

    Best will be:

    link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'
    
    0 讨论(0)
  • 2020-12-22 21:22

    Easy:

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

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

    0 讨论(0)
提交回复
热议问题