我已经在几个目录中组织了我的javascript文件,我发现了以下奇怪的行为.鉴于以下树:
+ app
+ assets
+ javascripts
+ common
+ public
+ common
+ home
- home.js
Run Code Online (Sandbox Code Playgroud)
home.js 看起来像这样:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory ../../jquery_plugins
//= require_directory ../../common
//= require_directory ../common
//= require_self
Run Code Online (Sandbox Code Playgroud)
现在的诀窍在于jquery_plugins目录.我把vendor/assets/javascripts它放在里面(当我检查时,它包含在资产加载路径中Rails.application.config.assets.paths).当我这样做时,我得到错误:require_tree argument must be a directory.当我移动该目录时,app/assets/javascripts一切正常.
有没有人知道我做错了什么?或者这是一个错误?
我理解,出于性能原因,最好让资产管道连接并缩小我的所有javascript,并将每个页面请求发送到整个批次.这很公平
但是,我的一堆javascript就像是将特定行为绑定到特定的页面元素之类的东西
$('button').click(function(e) { $('input.sel').val(this.name); }
Run Code Online (Sandbox Code Playgroud)
如果我知道这个代码只在那个页面上执行,我会感觉更舒服 - 而不是在其他页面上可能巧合地使用相同ID或匹配相同选择器的元素人们如何处理这个?
我宁愿不把所有这些东西都放在元素内联中,只是因为当它长度超过两行时,保持javascript正确缩进到.html.erb文件中是比它需要更多的工作
我的应用程序中的每个页面请求都会删除大约30个资产(JavaScripts和图像),这是供应商/应用程序/东西中的第三方资源.当然,所有的JavaScript在生产中都是合并和缩小的,所以它很好,但在开发中,它真的很慢.像8秒加载页面,只会变得更糟.这是正常的,可以做些什么来加快它?
我尝试将图像从assets /移动到public /,但它对它们的影响也很慢.每秒约3-4个文件.
这是在带有WEBrick和Unicorn的快速Macbook Pro上发生的.
我想知道,每次部署时如何将预编译资产上传到Cloudfront
ruby-on-rails amazon-web-services ruby-on-rails-3 asset-pipeline amazon-cloudfront
我在app/assets/javascripts下更改了一个JS文件,但它仍然是相同的.我删除了文件并重新创建,但内容仍然是旧的.这是我的development.rb文件:
App::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
config.serve_static_assets = false
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable …Run Code Online (Sandbox Code Playgroud) 如何在生产中使用config.assets.precompile只包含'lib/assets/javascripts','lib/assets/stylesheets','vendor/assets/javascripts'和'vendor/assets/stylesheets'中的文件?
基本上是这样的:
config.assets.precompile += %w( pagespecific.js anotherpage.js )
Run Code Online (Sandbox Code Playgroud)
但过去常常将文件包含在不是"app/assets/javascripts"或"app/assets/stylesheets"的特定目录中.
*编辑:添加我最终用于页面特定js的解决方案
config.assets.precompile += ['pages/*.js']
Run Code Online (Sandbox Code Playgroud) 看来现在在Rails 4中使用资产管道和sprocket-rails gem,当处理图像时,它们的文件名附加了像css和javascript这样的md5指纹.虽然这很有意义,因为md5指纹非常棒,但是从javascript访问该图像变得越来越困难.在rails 3.2中,我可以访问图像/assets/image_name.jpg并且它可以正常使用,但是在轨道4中资产不存在,它只存在名称中的md5指纹.
我知道rails提供帮助来通过erb访问图像,<%= asset-url("image_name.jpg") %>但这在javascript中不太理想,因为我没有在我的js中使用erb.有很多方法可以通过在视图中使用数据属性或在我的视图中使用脚本标记并设置一些全局变量来解决这个问题,但我正在寻找一个很好的解决方案,如果它存在的话.
任何帮助表示赞赏,谢谢.
我有两个问题.
我是否错误地假设我的所有javascripts都应该在rails 3.1中被压缩成application.js,即使在开发模式下也是如此?
如果没有,那么为什么我的标签包含我的所有30个javascripts并且需要加载?
我的application.js文件如下所示:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
Run Code Online (Sandbox Code Playgroud)
在浏览器中,它呈现为:
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the …Run Code Online (Sandbox Code Playgroud) 在这里坚果.我正在开发一个rails应用程序,我正在使用twitter-bootstrap-rails gem以便在我的应用程序中包含Twitter Bootstrap样式.这个gem在app/assets/stylesheets中生成一个名为'bootstrap_and_overrides.css.less'的文件,我一直用它来修改一些引导变量并包含我自己的CSS覆盖.
一切都很好,直到今天.出于某种原因,我今天对此文件所做的更改将保存到文件中,但Rails仍在提供该文件的旧版本!我搜索过,发现文件中没有任何预编译版本(公共/资产中没有)...只有我修改过的资产/样式表中的那个.对于应用程序中的目录,一切看起来都很好,但是当我启动rails服务器,加载页面,并使用元素检查器查看样式表时,它使用旧版本的'bootstrap_and_overrides.css.less'我已删除的规则.我已经在我的浏览器中关闭了缓存,并在4种不同的浏览器中尝试了它,所以我很确定这不是浏览器缓存的结果.
rails资产管道似乎只提供不存在的文件版本!有没有人知道为什么会发生这种情况?
web-applications ruby-on-rails browser-cache asset-pipeline twitter-bootstrap
在尝试为chrome创建源地图后,我从heroku收到以下警告http://blog.vhyza.eu/blog/2013/09/22/debugging-rails-4-coffeescript-and-sass-source-files -in-google-chrome / 它们表示什么,如果有的话,我应该改变什么?
我的gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
#gem 'sqlite3'
gem 'pg'
gem 'devise'
gem 'font-awesome-rails'
gem 'bootstrap-sass', github: 'thomas-mcdonald/bootstrap-sass'
gem 'simple_form', git: 'https://github.com/plataformatec/simple_form.git'
gem 'carrierwave'
gem 'fog', '~> 1.3.1'
gem 'mini_magick'
gem 'rails_12factor'
gem 'friendly_id', '~> 5.0.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
gem 'bourbon'
group :development do
gem 'sass-rails-source-maps'
end
# Use …Run Code Online (Sandbox Code Playgroud)