我的application.js无法正常工作,而application.css也不会在Rails中更新 [英] My application.js won't work and application.css won't update in Rails
问题描述
我将我的应用程序配置为可以在Heroku上运行,但是遇到了问题.在开发模式下,当我更改CSS的一部分并刷新浏览器时,我的CSS不会加载.
I configured my application to work on Heroku, but I'm having a problem. In development mode, when I change a part of the CSS and refresh the browser, my CSS doesn't load.
我检查了日志,并且未加载application.css.我需要每次在开发模式下加载它.另外,我的application.js停止工作,我不知道为什么.
I checked the logs, and the application.css doesn't load. I need it to load every time in development mode. Also, my application.js stopped working and I dont know why.
development.rb:
development.rb:
# Do not compress assets
config.assets.compress = false
config.assets.precompile += %w( login.css )
# Expands the lines which load the assets
config.assets.debug = true
enviroment.rb:
enviroment.rb:
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.assets.initialize_on_precompile = true
我使用 $ rake asset:precompile RAILS_ENV = development
手动编译资产,并且除了application.js代码之外,它都可以运行:
I compile the assets manually using $ rake assets:precompile RAILS_ENV=development
and it works except for the application.js code:
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery.reveal
$(document).ajaxStart(function() {
$( "#load_img" ).animate({
height:"toggle",
opacity:"toggle"
});
});
$(document).ajaxSuccess(function() {
$( "#load_img" ).fadeOut('fast');
});
$(document).ready(function() {
setTimeout(
function() {
$("#flash_messages").fadeOut(3000)
},
2500);
});
一切正常,然后再部署到Heroku.
Everything worked before deploying to Heroku.
推荐答案
您不需要 application.js
文件本身.像这样使用 require_self :
You don't require the application.js
file itself. Use require_self like so:
//= require jquery
...
//= require_self
$(function() {});
还非常确定您应该在include的末尾与 application.js
文件本身中的代码开头之间有一个换行符.
Also pretty sure you're supposed to have a line break between the end of the includes and the start of the code in the application.js
file itself.
也是这一行:
config.assets.precompile += %w( login.css )
可能应该在 application.rb
中,而不仅仅是在 development.rb
中,尽管如果在部署到Heroku之前以开发模式手动预编译资产也没关系.
should probably be in application.rb
and not just development.rb
, although if you are manually precompiling assets in development mode before deploying to Heroku it shouldn't matter.
这篇关于我的application.js无法正常工作,而application.css也不会在Rails中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!