我可以从 gem 内部提供资产吗

发布于 2024-12-28 10:11:48 字数 97 浏览 3 评论 0原文

我想将一些常见的资源(如 css、js 和图标图像)打包到 gem 中供我个人使用。

我可以直接使用 gem 内部的资源,还是必须让生成器将它们移动到主应用程序中?

I would like to package some common assets like css, js, and icon images into a gem for my personal use.

Can I use the assets from inside of the gem directly, or do I have to have a generator move them into the main app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2025-01-04 10:11:48

你需要做的是:

  • 制作轨枕:

    模块 MyGemName
      模块导轨
        类引擎< ::轨道::引擎
        结尾
      结尾
    结尾
    
  • 将它们放入一个原本是正确资源路径的目录中,例如 lib/assets/stylesheets

  • 使用 sprocket 包含 javascript:

    //= 需要“foobar”
    
  • 使用 sass 包含样式表:

    @import "foobar";
    
  • 如果您在样式表中引用图像,请使用 sass 函数 image-url

    <前><代码>.widget {
    背景图像: image-url("widget-icon.png");
    }

assets 目录的行为应该与您自己的应用程序中的行为完全相同。

您可以在 formalize-rails 中找到一个示例,其中包含样式表、javascript 和图像。

What you need to do is:

  • Make a railtie:

    module MyGemName
      module Rails
        class Engine < ::Rails::Engine
        end
      end
    end
    
  • Put them in a directory that would otherwise be a proper asset path, like lib/assets/stylesheets.

  • Use sprockets to include javascripts:

    //= require "foobar"
    
  • Use sass to include stylesheets:

    @import "foobar";
    
  • Use the sass function image-url if you refer to images inside your stylesheets:

    .widget {
      background-image: image-url("widget-icon.png");
    }
    

The assets directory should behave exactly the same as if it was inside your own application.

You can find an example in formalize-rails, which has stylesheets, javascripts and images.

深海蓝天 2025-01-04 10:11:48

使用 Rails 3.2,您可以创建一个引擎并将资源放入资源目录中,系统将自动获取它们。但请注意,如果您使用生成器创建可安装引擎,它将在 javascript、图像和样式表下创建命名空间目录。不要将您的内容放在这些子目录中,否则父应用程序将找不到它们。只需将它们直接放入 JavaScript、图像或样式表中即可。

With Rails 3.2 you can create an engine and put the assets in the assets directory where they'll be automatically picked up. Beware though if you create a mountable engine using the generator, it'll create namespaced directories under javascripts, images, and stylesheets. Don't put your stuff in those subdirectories or the parent app won't find them. Just put them directly in javascripts, images, or stylesheets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文