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

查看:20
本文介绍了如何在布局之外的 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 带有 yield 以在头部添加额外的标签.

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天全站免登陆