2016-03-30 69 views
0

這裏是我的代碼:我不能讓jQuery的文件工作

$('li.food').addClass('red'); 

網站是在生產。我在app/assets/javascripts文件夾中創建了一個jQuery文件hide.js

這是我application.html.erb文件:

<head> 

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 

<%= javascript_include_tag 'hide', 'data-turbolinks-track' => true %> 
<%= stylesheet_link_tag 'hide', media: 'all', 'data-turbolinks-track' => true %> 

<%= csrf_meta_tags %> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<%= render 'layouts/shim' %> 

</head> 

這是Heroku的日誌中的錯誤消息:

ActionController::RoutingError (No route matches [GET] "/javascripts/hide.js"): 
vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `block in tagged' 
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:26:in `tagged' 
vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/request_id.rb:21:in `call' 

這是我application.js文件

//= require social-share-button 
//= require jquery 
//= require jquery_ujs 
//= require hide 
//= require hide.js 
//= require bootstrap 
//= require turbolinks 
//= require_tree . 

的Gemfile

source 'https://rubygems.org' 

gem 'rails',     '4.2.2' 
gem 'bootstrap-sass',   '3.2.0.0' 
gem 'bcrypt',     '3.1.7' 
gem 'faker',     '1.4.2' 
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave' 
gem 'mini_magick',    '3.8.0' 
gem 'fog',      '1.36.0' 
gem 'will_paginate',   '3.0.7' 
gem 'bootstrap-will_paginate', '0.0.10' 
gem 'sass-rails',    '5.0.2' 
gem 'uglifier',    '2.5.3' 
gem 'coffee-rails',   '4.1.0' 
gem 'jquery-rails',   '4.0.3' 
gem 'turbolinks',    '2.3.0' 
gem 'jbuilder',    '2.2.3' 
gem 'sdoc',     '0.4.0', group: :doc 
gem 'fog-aws' 
gem 'activerecord-reputation-system', require: 'reputation_system' 
gem 'social-share-button', '~> 0.1.6' 

group :development, :test do 
    gem 'sqlite3',  '1.3.9' 
    gem 'byebug',  '3.4.0' 
    gem 'web-console', '2.0.0.beta3' 
    gem "spring", group: :development 
end 

group :test do 
    gem 'minitest-reporters', '1.0.5' 
    gem 'mini_backtrace',  '0.1.3' 
    gem 'guard-minitest',  '2.3.1' 
end 

group :production do 
    gem 'pg',    '0.17.1' 
    gem 'rails_12factor', '0.0.2' 
    gem 'puma',   '2.11.1' 
end 

Chrome的錯誤

Failed to load resource: the server responded with a status of 404 (Not Found) ...for stylesheets/hide.css 
Failed to load resource: the server responded with a status of 404 (Not Found) ...for javascripts/hide.js 

我試過bundle exec rake assets:precompileHeroku run bundle exec rake assets:precompile

我沒有任何問題運行引導jquery組件,如Carousel。

從搜索,好像所有我應該有做的就是把應用程序/資產/ JavaScript和Rails的hide.js應該照顧的休息,:(

+0

您已隱藏在您的需求樹中,但它是否包含在應用程序中? – toddmetheny

+0

你可以試試從你的'application.js'去除'// = require hide // = require hide.js'。它們通過'// = require_tree'自動包含到'application.js'中。 – dp7

+0

是的,它仍然不起作用。 –

回答

0

的各種答案被發現here

$(document).on('page:change', function() { 
// Actions to do 
}); 
0

從刪除此行您application.js中

//= require hide.js 

此外,如果你已經在你的application.js加入隱藏的js文件所以沒必要把它分開使用

<%= javascript_include_tag 'hide', 'data-turbolinks-track' => true %> 
+0

仍然無法使用。 –