简体   繁体   English

ERB文件中的相对文件名

[英]Relative filename in ERB files

I am putting my ERB files in app/view. 我将我的ERB文件放入应用程序/视图中。 The problem is that sometimes I make mistakes when including CSS and JS files. 问题是,有时在包含CSS和JS文件时会出错。 I refer to them as "js/include.js" or "css/default.css" instead of /js/include.js and /css/default.css 我将它们称为“ js / include.js”或“ css / default.css”,而不是/js/include.js和/css/default.css

However, these files are located in the public directory not the app/views directory so as a result the page breaks. 但是,这些文件位于公共目录中,而不位于app / views目录中,因此页面中断。

Is there a way to change the default behavior so that it looks in public folder whenever I refer to these files relatively? 有没有一种方法可以更改默认行为,以便每当我相对引用这些文件时,它就会在公用文件夹中显示?

If you stick with the Rails conventions you'll save yourself some grief. 如果您坚持使用Rails约定,就会为自己省去一些麻烦。 Use stylesheet_link_tag and javascript_include_tag in your layouts. 在布局中使用stylesheet_link_tagjavascript_include_tag And don't scatter css and js includes around your views. 并且不要分散css和js包含在您的视图周围。 Use custom layouts instead. 请改用自定义布局。

For example, if you have an admin interface with a different look and different behavior you could add app/views/layouts/admin.html.erb. 例如,如果您拥有一个具有不同外观和行为的管理界面,则可以添加app / views / layouts / admin.html.erb。 Then, in AdminController specify that it should use the admin layout. 然后,在AdminController中指定它应使用admin布局。 You can also specify the layout at the controller action level if you need to. 如果需要,还可以在控制器操作级别指定布局。

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

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