link_to image tag. how to add class to a tag

前端 未结 11 1877
失恋的感觉
失恋的感觉 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! =)

提交回复
热议问题