【问题标题】:Display an image in a Popover Bootstrap with ruby code via Cloudinary/CarrierWave gem通过 Cloudinary/CarrierWave gem 在带有 ruby​​ 代码的 Popover Bootstrap 中显示图像
【发布时间】:2016-06-28 01:13:42
【问题描述】:

您好,这是我在此模板中提出的第四个问题。请有人帮助我:)

我在抽认卡上工作,当我在上面触发光标时,我必须在弹出窗口中显示一个图像。

这是我的代码

<div class="idea">
   <i class="fa fa-lightbulb-o fa-lg popover-img" 
   data-content=<%="#{cl_image_tag(@hiragana.upload, :width => 260, :height => 340)}"%>></i>
</div>

我把它放在数据内容中。

我使用这个 javascript 在我的 layout/application.html.erb 中调用它

<script >
      $(document).ready(function(){
        $(".popover-img").popover({
          html: true,
          trigger: "hover",
          // content: "",
          placement:'bottom'
        });
      })

结果是无法显示图片

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap carrierwave popover cloudinary


    【解决方案1】:

    您遇到的问题是您正在调用的标签。 cl_image_tag 是一个生成 img html 标记的助手。但是您的 js 已对内容行进行了注释,所以我猜您已经尝试了几件事。

    如果您选择使用标签创建器,并且我理解您为什么会这样做,它会使生成基于大小的 cloudinary url 更加简单,那么您需要在数据元素中用单引号正确转义它,然后调用它并在js中渲染整个html标签。

    例如我有这样的事情:

    <span class="popover-trigger fa fa-4x fa-ban "
         data-toggle="popover"
         data-content='<%= cl_image_tag(current_user.avatar, 
            :width => 250, :height => 200) %>'></span>
    

    然后你的 JS 看起来像:

    $('[data-toggle="popover"]').popover({
      html: true,
      trigger: 'hover',
      content: function () {
        return $(this).data('content');
      }
    });
    

    这将在悬停时调用它,并将 data-content 属性中的值设置为在弹出内容区域中呈现,只要它被正确引用。

    -- 基于你的 cmets ---

    我认为您的视图代码可能如下所示:

    <% @hiraganas.each do |hiragana| %> 
    
    <div class="idea">
       <i class="fa fa-lightbulb-o fa-lg popover-img" 
       data-toggle="popover"
       data-content=<%="#{cl_image_tag(hiragana.upload, 
       :width => 260, :height => 340)}"%>></i>
    </div>
    <%end%>
    

    【讨论】:

    • 感谢您的帮助并理解我的问题。我试了一下,效果很好!我只是用你的方法current_user.avatar 来挖掘@hiragana.upload。保护你。如果我想迭代,我只是在hiragana.upload 之前删除@ 吗?
    • 我的意思是我在我的显示视图中这样做 &lt;span class="popover-trigger fa fa-4x fa-ban " data-toggle="popover" data-content='&lt;%= cl_image_tag(@hiragana.upload, :width =&gt; 260, :height =&gt; 340) %&gt;'&gt;&lt;/span&gt; 而在我的索引中我必须这样做 &lt;% @hiraganas.each do |hiragana| %&gt; &lt;span class="popover-trigger fa fa-4x fa-ban " data-toggle="popover" data-content='&lt;%= cl_image_tag(hiragana.upload, :width =&gt; 260, :height =&gt; 340) %&gt;'&gt;&lt;/span&gt; &lt;% end %&gt;
    • 是的,您分享的第二个代码是正确的,在迭代块内hiragana 确实不应该包含@。顺便说一句 - 请注意,cl_image_tag(hiragana.upload, :width =&gt; 260, :height =&gt; 340) 会生成一个图像标签,该标签会从 Cloudinary 请求全尺寸图像并在客户端执行大小调整(浏览器端调整大小)。为了告诉 Cloudinary 在交付前调整图像大小,您还应该包括裁剪模式。例如,cl_image_tag(hiragana.upload, :width =&gt; 260, :height =&gt; 340, :crop =&gt; :fill)
    猜你喜欢
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2020-01-17
    • 2020-05-20
    • 2018-06-09
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多