DRY MVC 声明式最小控制器特定资产管道

发布于 2025-01-06 08:14:45 字数 875 浏览 2 评论 0原文

有多种方法可以在 Rails 中包含特定于控制器的资源:

一种非 DRY 的选项是 add = yield :head< /code> 位于布局中,content_for(:head) { ... } 位于每个顶级视图中。 如果资源是特定于控制器的,每个只能指定一次控制器,而不是在每个视图中。当然,这种方法对于视图特定的资产来说非常棒。

非声明性的第二个选项是添加与控制器名称相对应的资产,如果它存在。我不应该检查某些东西是否存在,而应该简单地说(在适当的情况下)它存在并且必须包含在内。另外,我不确定是否会缓存响应以避免运行时性能受到影响。从积极的一面来看,这种方法不需要对视图或控制器进行任何更改,但它可能会带来名称冲突的可能性,尤其是对于旧模型。

第三种选择是将某种类型的所有资源包含在单个文件中。浏览器不应下载不需要的资源,这将使调试应用程序变得更加困难。如果总资产规模仍然可控,那么这个选项就很好。

有没有某种方法可以以声明方式单独的文件中以DRY的方式包含单个特定于控制器的资产,而无需使用很少的代码打破MVC模型?

There are several ways to include controller-specific assets in Rails:

One option which is not DRY is to add = yield :head in the layout and content_for(:head) { ... } in every top-level view. If an asset is controller-specific, it should be specified only once per controller, not in each view. Of course, this approach is awesome for view-specific assets.

A second option which is not declarative is to add an asset corresponding to the controller name if it exists. Instead of checking whether something exists, I should simply say (where appropriate) that it exists and must be included. Also, I'm not sure if the response would be cached to avoid a runtime performance hit. On the positive side, this approach wouldn't require any changes to views or controllers, but it might open up the possibility for name clashes, especially with legacy models.

A third option is to include all assets of a type in a single file. Browsers shouldn't download assets they don't need, and this would make it more difficult to debug the application. This option would be fine if the total asset size is still manageable.

Is there some way to declaratively include a single controller-specific asset in a separate file in a DRY way without breaking the MVC model using very little code?

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

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

发布评论

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

评论(1

轻拂→两袖风尘 2025-01-13 08:14:45

如果您在应用程序布局中使用以下包含命令,Rails 将仅将控制器特定资产文件中的代码提供给指定的控制器:

<%= javascript_include_tag params[:controller] %>
<%= stylesheet_link_tag params[:controller] %>

我怀疑如果您这样做,您还需要执行以下操作:

  • 仍然包含 < ;%= javascript_include_tag :application %><%= stylesheet_link_tag :application %> 获取所有跨控制器资源
  • 检查 require_tree 如何. 指令用于确保控制器特定资源不会同时被 application.css<%= stylesheet_link_tag params[:controller] %>< 加载/code> 事实上,您可能需要删除 require_tree . 并将任何跨控制器表直接加载到 application 文件中

请参阅第 2 节中的 Rails 资产管道指南 了解更多信息。

Rails will serve only the code in the controller specific asset files to the specified controller if you use the following include commands in your application layout:

<%= javascript_include_tag params[:controller] %>
<%= stylesheet_link_tag params[:controller] %>

I suspect if you do this you will need to also do the following:

  • Still include the <%= javascript_include_tag :application %> and <%= stylesheet_link_tag :application %> to get all your cross controller assets
  • Check how the require_tree . directives work to ensure that the controller specific assets are not being loaded both by the application.css and the <%= stylesheet_link_tag params[:controller] %> in fact you may need to remove the require_tree . and load any cross controller sheets directly into the application files

See the Rails Guide on the Asset Pipeline in section 2 for more information.

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