如何在版面之外的erb中包含CSS或JavaScript? [英] How to include a css or javascript in an erb that is outside the layout?

查看:40
本文介绍了如何在版面之外的erb中包含CSS或JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我正在写我的第一个rails应用程序。

Sorry for the slightly noobish question, as I am writing my first rails app.

我对布局视图有所了解,但是如果您正在使用它们,有什么办法可以包含特定于视图的js或css文件?例如,我有 layouts / products.html.erb ,对于 products / edit.html.erb 我想要 products_edit.css ,但我不希望所有产品视图都使用该CSS,最佳做法是什么?

I get the idea of the layout view, but if you are using them, is there any way to include a view specific js or css file? For example, I have layouts/products.html.erb, and for products/edit.html.erb I want products_edit.css, but I don't want that css for all product views, what is the best practice to accomplish that?

推荐答案

如果您有通用的edit.css文件,建议您在布局中使用if

If you have a generic edit.css file, I would suggest an if in your layout

<%= stylesheet_link_tag 'edit' if params[:action] == 'edit' %>

否则,您可以使用 content_for 的收益为 code>将其他标签添加到头部。

Otherwise you can use content_for with a yield to add additional tags into the head.

layout.html.erb

<head>
  ...
  <%= yield(:header) if @content_for_header %>
</head>

products / edit.html.erb

<% content_for :header do -%>
  <%= stylesheet_link_tag 'edit_product' %>
<% end -%>

这篇关于如何在版面之外的erb中包含CSS或JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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