Rails 3 - link_to with image_tag + text

大城市里の小女人 提交于 2020-01-11 22:51:29

问题


<%= link_to ((image_tag 'image.png'), 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>

This part of code will generate me image.png as a link. I would need to this image append some text (image + text), I tried something like a:

<%= link_to ((image_tag 'image.png', 'text'), 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>

And similar ways, but each of these attempts ended with an error message about bad syntax... Could anyone help me, please, how I should set it right?

Thanks in advance.


回答1:


Try this.

<%= link_to image_tag('/images/image.png') + "some extra text", url_for({:controller => 'controller_name', :action => 'action_name'}), :class => 'quick', :remote => true %>



回答2:


A slightly sexier solution?

<%= link_to image_tag("image.png", :alt => "Image Description", :class => "css"), root_path %>



回答3:


Try this:

<%= link_to (image_tag('image.png') + text, 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>

The first argument is the text part and with image_tag you create HTML, but you can easily append stuff.




回答4:


I used the following and it works just fine:

<%= link_to image_tag("logo.jpg"), controller: 'welcome' %>


来源:https://stackoverflow.com/questions/7414842/rails-3-link-to-with-image-tag-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!