DRY MVC声明性的最小控制器特定资产管道 [英] DRY MVC declarative minimal controller-specific asset pipeline

查看:84
本文介绍了DRY MVC声明性的最小控制器特定资产管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

不是DRY的一个选项是在布局中添加= yield :head 中的content_for(:head) { ... }每个顶级视图.如果资产是特定于控制器的,则每个控制器仅应指定一次,而不应在每个视图中都指定一次.当然,这种方法对于 view 特定资产来说真是棒极了.

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.

有没有办法以声明方式 DRY 单独文件中包含单个控制器特定资产不会用很少的代码破坏 MVC 模型的方法?

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?

推荐答案

如果在应用程序布局中使用以下包含命令,则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:

  • 仍然包含<%= javascript_include_tag :application %><%= stylesheet_link_tag :application %>以获得您的所有跨控制器资产
  • 检查require_tree .指令的工作方式,以确保同时不由application.css<%= stylesheet_link_tag params[:controller] %>加载控制器特定的资产,实际上您可能需要删除require_tree .并加载任何交叉控制器工作表直接放入application文件
  • 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

有关更多信息,请参见第2部分中的资产管道的路轨指南.

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

这篇关于DRY MVC声明性的最小控制器特定资产管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆