【问题标题】:Rails Helper that identifies file type of url in linkRails Helper 识别链接中 url 的文件类型
【发布时间】:2010-09-07 14:07:02
【问题描述】:

如果文件是 PDF,我正在寻找一个可以改变 link_to 行为的助手,即显示一个工具提示。

我如何编写一个仅当文件是 pdf 时才覆盖 link_to 的帮助程序?

【问题讨论】:

    标签: ruby-on-rails helpers


    【解决方案1】:

    为什么要更改link_to 的行为?对于工具提示,您需要设置 title 属性,或者使用 javascript。所以你不需要改变默认行为。

    <%= link_to "A PDF document",
                "/some_file.pdf",
                :title => "Tooltip in Most Browsers"
                #, :class => "pdf %>
    

    如果你想要一些花哨的酷工具提示,请尝试 google “jQuery tootltip”或类似的东西。

    您还可以添加:class =&gt; "pdf",以便能够找到所有带有javascript 的pdf 链接。在 jQuery 中它可能看起来像这样:

    $('a.pdf');
    

    如果你创建了很多这样的链接,你可以用这个助手DRY它:

    def link_to_pdf(name,path,title)
      link_to name, path, :title => title, :class => "pdf"
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2011-08-02
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多