【问题标题】:Use an image from the asset pipeline inside of a link_tag在 link_tag 内使用资产管道中的图像
【发布时间】:2014-07-13 17:17:22
【问题描述】:

我想创建一个使用资产管道中的图像的链接。图片存储在assets/images/github_icon.png

不带图片的链接标签:

<%= link_to "Log in with Github", omniauth_authorize_path(resource_name, provider) %>

链接标签与图像(我如何创建这个?):

<%= link_to ?????, omniauth_authorize_path(resource_name, provider) %>

过去我在链接标签中添加html时使用raw()

<%= link_to raw("<p>Hey kids!</p>"), ... %>

但我不确定如何将raw()img_tag("github-icon.png") 一起使用。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 asset-pipeline


    【解决方案1】:

    link_to

    两种方式:

    <%= link_to image_tag("github_icon.png"), path %>
    

    <%= link_to path do %>
        <%= image_tag "github_icon.png" %>
    <% end %>
    

    --

    像许多 Rails 助手一样,link_to 非常灵活 - 您只需将正确的参数传递给它。您正在尝试传递 img_tag(它不作为方法存在)和 raw()(它不会做任何事情)。

    你应该使用image_tag,不需要使用raw

    【讨论】:

    • 啊,秒杀我:D
    • 您应该在回答中提到您得到的是code block,而不是loop
    【解决方案2】:

    使用link tag with block。试试

    <%= link_to omniauth_authorize_path(resource_name, provider) do %>
      Log in with Github
      <%= image_tag "github_icon.png" %>
    <% end %>
    

    注意 link_to 块版本不接受文本,因此您必须添加文本 在块中使用 Github 登录

    【讨论】:

      【解决方案3】:

      你可以试试这个方法:

      1. link_to image_tag("imageName.png", :border =&gt; 0), {:action =&gt; 'actionName', :controller =&gt; 'controllerName'}, {:class =&gt; 'className'}

      或者你可以试试这个:

      <%= link_to href: '/url/for/you' do %>
          <%= image_tag 'yourImage.jpg', width: 136, height: 67, alt: 'WIN!'%>
      <% end %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 2016-02-10
        • 1970-01-01
        • 2014-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多