简体   繁体   English

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

[英]DRY MVC declarative minimal controller-specific asset pipeline

There are several ways to include controller-specific assets in Rails: 有几种方法可以将特定于控制器的资产包含在Rails中:

One option which is not DRY is to add = yield :head in the layout and content_for(:head) { ... } in every top-level view. 一个不是DRY的选项是在布局中添加= yield :head每个顶级视图中 添加 content_for(:head) { ... } 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? 是否有某种方法可以以DRY方式声明性地将单个控制器特定资产包括单独的文件中,而无需使用很少的代码即可破坏MVC模型?

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: 如果在应用程序布局中使用以下包含命令,Rails将仅将控制器特定资产文件中的代码提供给指定控制器。

<%= 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 仍然包括<%= javascript_include_tag :application %><%= stylesheet_link_tag :application %>以获得您的所有跨控制器资产
  • Check how the require_tree . 检查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 . 伪指令可以确保application.css<%= stylesheet_link_tag params[:controller] %>都不会加载控制器特定的资产,实际上您可能需要删除require_tree . and load any cross controller sheets directly into the application files 并将任何跨控制器工作表直接加载到application文件中

See the Rails Guide on the Asset Pipeline in section 2 for more information. 有关更多信息,请参见第2节中的资产管道上Rails指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM