我正在尝试在 application.js 下正确导入供应商 js 文件app/javascript/packs/application.js
这是当前的架构:
可以看到,我选择将供应商 js 文件放入app/javascrip/plugins/
我的结构application.js如下:
// Rails Internals
require("@rails/ujs").start();
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
// Libraries/Vendor-plugins
require("jquery");
import 'bootstrap';
require('../plugins/chosen.min.js');
require('../plugins/magnific-popup.min.js');
require('../plugins/owl.carousel.min.js');
require('../plugins/rangeSlider');
require('../plugins/sticky-kit.min.js');
require('../plugins/masonry.min.js');
require('../plugins/mmenu.min.js');
require('../plugins/tooltips.min.js');
require('../plugins/custom');
// Custom functions
// import { myFunction } from '../components/myScrip';
document.addEventListener('turbolinks:load', () => {
});
Run Code Online (Sandbox Code Playgroud)
custom.js通过这样做,位于 中的文件应该可以访问 jQuery app/javascript/plugins/custom.js,对吧?
为了清楚起见, 的custom.js形状如下:
(function ($) {
"use strict";
$(document).ready(function () {
// ...
}
})(this.jQuery);
Run Code Online (Sandbox Code Playgroud)
但是当我运行服务器时,找不到 jQuery:
yarn add jquery@3.5.1 bootstrap@3.4.1我通过(供应商需要的旧版本)添加了 jQuery …
I\xe2\x80\x99m 在具有资产管道的 Rails 7 项目中使用tailwindcss-rails gem。我需要重用 TailwindCSS 样式,例如:
\n.pagy-nav {\n @apply flex space-x-2;\n}\nRun Code Online (Sandbox Code Playgroud)\n我可以将此代码放入app/assets/stylesheets/application.tailwind.css文件中并且它可以工作:
@tailwind base;\n@tailwind components;\n@tailwind utilities;\n\n.pagy-nav {\n @apply flex space-x-2;\n}\nRun Code Online (Sandbox Code Playgroud)\n但是,我想将.pagy-nav代码放入单独的CSS文件pagy.css中(以更加干净/有条理)。有办法做到吗?
当我尝试使用命令部署我的rails应用程序时cap deploy它失败:
./config/deploy.rb:31:in'load':没有要加载的文件 - deploy/assets(LoadError)
这是由于
load 'deploy/assets'
我的capistrano deployment.rb文件中的行.该行应该在部署期间触发资产预编译.
据捆绑秀我正在使用:
为什么不工作?
我试图使用foot-rails gem 安装flot但是没有成功.
我添加gem "flot-rails"到我的gemfile并执行bundle install
然后我补充道
//= require jquery.flot
//= require jquery.flot.resize
Run Code Online (Sandbox Code Playgroud)
到我的/app/assets/javascripts/application.js文件
我验证了gem正确安装(gem show flot-rails)并发现javascripts都在gem的供应商目录中,但是我的app报告
找不到文件'jquery.flot'(在... path/app/assets/javascripts/application.js:12)
我选择手动将文件安装到我的资产目录中,但我想知道我的链轮有什么问题.
为什么sprockets解析引用?
我正在尝试第一次部署我的应用程序并遇到资产管道问题.当我运行时,bundle exec rake assets:precompile我得到以下错误;
cool_app@ip-10-248-17-168:/rails_apps/cool_app/current$ bundle exec rake assets:precompile
/home/cool_app/.rvm/rubies/ruby-1.9.3-p327/bin/ruby /rails_apps/cool_app/shared/bundle/ruby/1.9.1/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
Unexpected character '' (line: 13934, col: 1, pos: 403510)
Error
at new JS_Parse_Error (<eval>:1720:22)
at js_error (<eval>:1728:15)
at parse_error (<eval>:1824:17)
at next_token (<eval>:2070:17)
at handle_slash (<eval>:2024:32)
at Object.next_token [as input] (<eval>:2067:39)
at next (<eval>:2175:37)
at Object.semicolon [as 1] (<eval>:2221:38)
at prog1 (<eval>:2770:28)
at simple_statement (<eval>:2362:35)
(in /rails_apps/cool_app/releases/20121128034720/app/assets/javascripts/application.js)
Run Code Online (Sandbox Code Playgroud)
我认为这意味着我在我迷宫的javascript中有一个非法字符(那不是渲染)(应该早点部署!)但告诉我它在13934行的第1列似乎并不特别有用.我怎么去打猎呢?错误消息非常无益(我在这里也注意到关于这个主题的很多其他问题).
我不确定其重要性,但是如果我RAILS_ENV=development rake assets:precompile在我的开发机器上运行,那么任务似乎没有抱怨就会执行.我的服务器是Ubuntu 12.04,我的开发机器是OSX 10.8.我现在还不太了解Javascript运行时解决这个问题(但我正在寻找).
如何让asset_path返回绝对URL?
我需要像这样的元标记的绝对URL:
<meta property="og:image" content="<%= asset_path('logo_400x200.jpg') %>" />
Run Code Online (Sandbox Code Playgroud)
这里asset_path的问题是它输出一个带" //site.com/logo_400x200.jpg" 的URL,而meta标签需要一个绝对路径.
有关如何使用rails asset_path获取绝对路径的任何想法?
谢谢
我在rails项目上有以下JavaScript代码
// app/assets/javascript/simple_effects.js
$(function() {
// Hide completed tasks when checkbox is clicked. projects/show
$('#hide-completed').click(function() {
$('.task-completed').toggle();
});
// Change label to checkbox that hides completed tasks. project/show
$('#hide-completed').change(function(){
var c = this.checked ? ' Show completed tasks' : ' Hide completed tasks';
$('#checkbox-label').text(c);
});
});
Run Code Online (Sandbox Code Playgroud)
当我进入页面但仅在刷新之后,代码不会触发.我正在使用turbolinks.
我知道这个问题: Rails仅在页面重新加载后才加载我的javascript 但是我真的不知道是否可以在我的情况下使用该解决方案.谢谢!
编辑:这是我的application.js文件
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
Run Code Online (Sandbox Code Playgroud) 我可以在本地Rails 4.1项目中编译我的资产,如下所示:
$ RAILS_ENV=staging bin/rake assets:precompile
I, [2015-03-26T17:23:21.632726 #26321] INFO -- : Writing /MyProject/public/assets/active_admin-a46314b7dc1a06e662695c2093e33d96.js
I, [2015-03-26T17:23:21.748271 #26321] INFO -- : Writing /MyProject/public/assets/application-adc2c7c08b35fae05d8bfc73d1696dea.js
I, [2015-03-26T17:23:21.833131 #26321] INFO -- : Writing /MyProject/public/assets/active_admin-36d00173ec2474695cd54a330636bac9.css
I, [2015-03-26T17:23:21.851637 #26321] INFO -- : Writing /MyProject/public/assets/application-0930e157132302bc8b8666143a832af2.css
I, [2015-03-26T17:23:21.861676 #26321] INFO -- : Writing /MyProject/public/assets/debug-aeffd81aa2ee2b35a0d779b9335c9749.css
Run Code Online (Sandbox Code Playgroud)
也可以直接在服务器上使用rake创建资产:
user@server$ RAILS_ENV=staging bundle exec rake assets:precompile
I, [2015-03-26T17:40:11.761707 #30743] INFO -- : Writing /var/www/my_project/releases/20150326161842/public/assets/active_admin-a46314b7dc1a06e662695c2093e33d96.js
I, [2015-03-26T17:40:11.806881 #30743] INFO -- : Writing /var/www/my_project/releases/20150326161842/public/assets/application-adc2c7c08b35fae05d8bfc73d1696dea.js
I, [2015-03-26T17:40:11.847215 #30743] INFO -- : Writing …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails asset-pipeline ruby-on-rails-4 capistrano3
我想要一个具有不同 css、javascript 和图像文件的单页浏览器,用作我的应用程序的登录页面。
我将我的 css 移动到名为 onepager 的子文件夹下的 vendor/assets 文件夹中。我使用了一个子文件夹,因为我只会将这些文件用于 onepager 而不会用于其他任何东西。
例如
vendor/assets/stylesheets/onepager/
Run Code Online (Sandbox Code Playgroud)
我创建了一个单独的布局,它引用了单独的 css 文件。> onepager.html.erb
<%= stylesheet_link_tag 'onepager', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'onepager', 'data-turbolinks-track' => true %
Run Code Online (Sandbox Code Playgroud)
在文件 onepager.js 和 onepager.css.scss 中,我希望它们引用 vendor/assets/xxx/onepager/ 文件夹中的文件。我添加了以下代码,但出现错误。
onepager.css.scss
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, …Run Code Online (Sandbox Code Playgroud) 我有一个Rails应用程序,application.js资产编译后需要超过1 MB。这使我的整个网站变慢。
我使用Apache,Rails 4,jQuery,相当繁重的JavaScript和AJAX。如果有人能指出正确的方向,我将不胜感激。
我需要获取位于assets/schemas/resource.jsonGrails 3服务中的静态资源的路径.
目前定义为
private final String SCHEMA = 'grails-app/assets/schemas/resource.json',
Run Code Online (Sandbox Code Playgroud)
这对development环境很好,但当然不适合生产(因为它将位于<app_root>/assets/resource.json.
我试图在我的案例中搜索如何利用资产管道,但到目前为止我真的不知道:P
提前致谢!
刚刚将 Rails 4.2 应用程序更新到 5.2.1,但无法将其推送到 Heroku。以下错误。我已经通过在我的 rack_timeout.rb 初始值设定项中添加以下内容来尝试这里和这里推荐的内容。
Rails.application.config.middleware.insert_before Rack::Runtime, Rack::Timeout, service_timeout: 5
Run Code Online (Sandbox Code Playgroud)
但这无济于事。想法?
$ git push heroku
...
remote: -----> Installing node-v8.10.0-linux-x64
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: rake aborted!
remote: NoMethodError: undefined method `timeout=' for Rack::Timeout:Class
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/config/initializers/rack_timeout.rb:2:in `<top (required)>'
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `load'
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `block in load'
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:281:in `load'
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/vendor/bundle/ruby/2.4.0/gems/railties-5.2.1/lib/rails/engine.rb:657:in `block in load_config_initializer'
remote: /tmp/build_c66056b406a6fcdc37b9dde3124992ce/vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:170:in `instrument' …Run Code Online (Sandbox Code Playgroud) 我正在将 Rails 从 5 升级到 6,但遇到了一个问题。我正在更新的应用程序没有资产(或控制器或视图),因为它只是使用 Grape 提供一个简单的 API。我不断遇到围绕资产管道的问题,而不是仅仅为了使其工作而添加垃圾,我想完全摆脱它。
我用 Gemfile 中的bundle update railswith进行了升级。gem "rails", "~> 6.0.3"我想知道是否有一种方法可以完全删除与资产管道相关的垃圾并将其关闭。
目前,我正在处理一条错误消息,抱怨文件丢失 - app/assets/manifest.js。我一直在寻找的解决方案几乎都是“创建该文件”,但这让我觉得这是尾巴在摇狗。
任何帮助,将不胜感激。
asset-pipeline ×13
ruby-on-rails ×10
jquery ×2
ruby ×2
capistrano ×1
capistrano3 ×1
css ×1
flot ×1
grails ×1
grails-3.0 ×1
heroku ×1
javascript ×1
rack ×1
sprockets ×1
tailwind-css ×1
turbolinks ×1
webpack ×1