Rails custom layouts - routingerror in production - javascript

I have an admin section of my site that uses an 'admin' layout.
class Admin::BaseController < ActionController::Base
layout "admin"
in layouts/admin.html.slim
= stylesheet_link_tag "admin", :media => "all"
= javascript_include_tag "admin"
I get an error for the admin.js and admin.css
Started GET "/stylesheets/admin.css" for x.x.x.x at 2013-10-25 13:18:16 +0000
F, [2013-10-25T13:18:16.505425 #31550] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/stylesheets/admin.css"):
this works perfectly in development and the frontend assets are loading normally in production, what can the problem be?

The reason is that your rails server (webrick, thin, ...) serves your assets uncompiled, where it looks for it in several places (like app/assets or vendor/assets). In production, rails assumes that the production webserver handles asset serving, which are served from public/.
Since compilation on runtime – or delivering them uncompiled at all – slows down page serving, they are compiled into the name of its known manifest-files that reference your styles (that is application.{css/js} by default), which you do by invoking rake assets:precompile.
Add config.assets.precompile += %w( admin.css ) to config/application.rb or config/environments/production.rb to add your admin.css manifest so rails know that it also has to compile that one. Then run rake assets:precompile (if you've put it into the later, you may need to add RAILS_ENV=production).

Related

Rails 3.2.13 - No route matches [GET] "/js/jquery.fancybox.js"

I was assigned with the task of integrating new designs into an old app , the app was built on 3.2.8 and i did an upgrade to 3.2.13 . What puzzles me is the js part , i have done everything i could by searching on SO and vice versa but no luck .Already looked into this and this one but somehow the js isn't being served .Following are the parts of code that might be useful -
app/views/layouts/application.html.erb :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<%= javascript_include_tag "html5shiv.js" %>
<%= javascript_include_tag "fancybox.js" %>
app/views/home/index.html.erb
<script type="text/javascript" src="/javascripts/html5shiv.js"></script>
<script type="text/javascript" src="/javascripts/fancybox.js"></script>
Error I get:
Started GET "/js/jquery.fancybox.js" for 127.0.0.1 at 2014-05-14 17:17:33 +0530
ActionController::RoutingError (No route matches [GET] "/js/jquery.fancybox.js"):
Updated Error -
Started GET "/assets/html5shiv.js" for 127.0.0.1 at 2014-05-14 17:17:33 +0530
Served asset /html5shiv.js - 304 Not Modified (0ms)
[2014-05-14 17:17:33] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Any suggestion or pointers are most welcome . More details will be provided on asking .Thanks in advance , hoping to get it solved :)
First of all I'm not sure why you're including your scripts twice but I would recommend against it. Secondly, are you using the asset pipeline? If you are (and I strongly recommend that you do) simply add the following to your application.js file:
//= require html5shiv
//= require fancybox
And the correct files should be included in your concatenated javascript file.
Also, the error you're receiving doesn't seem to be connected to the code you posted (as you specify no links to assets living under /js), but I do believe that when you attempt to link to scripts living at /javascripts (as opposed to assets/javascripts) you might get a routing error like the one you posted.
So, remove the references to your scripts and simply add them to your manifest (application.js) and you should be fine.

Rails 3.2 continues to serve OLD version of JavaScript asset file - Development Env

I have a Rails 3.2 application, and in it I have a Javascript file (app.'s) which appears to be 'stuck' at a particular, past, version. I've updated the file many times, changing its contents significantly, and being sure each time to Save the file - to no effect. I've rebooted the machine multiple times (thinking at least THAT should clear out the cobwebs) all to no avail. I'm at my wits end trying to think of ways to un-stick this.
I can see that the file is being served, each time I visit the page:
Started GET "/dev-assets/app.js?body=1" for 172.16.0.12 at 2014-04-24 16:49:36 -0700
Served asset /app.js - 200 OK (1ms)
And then later in the same test run (not modified - as I expect):
Started GET "/dev-assets/app.js?body=1" for 172.16.0.10 at 2014-04-24 16:51:31 -0700
Served asset /app.js - 304 Not Modified (0ms)
But STILL it has the old contents (which, have a call to 'location.assign' which has since been removed - so the behavior should be significantly different than what I'm seeing.
I have these settings in my development.rb file (but this is not the complete file):
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# From Rails issue 10091 - attemp to keep from loading scripts
# multiple times
config.assets.prefix = "/dev-assets"
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
# Workaround documented in Rails issue 4145
config.serve_static_assets = false
Anyone have any idea how I can clear this old file?
Note - this is very similar to:
Clear the cache from the Rails asset pipeline
But none of those suggestions have helped me.
This isn't a Rails problem. Rails is serving up the correct file, as verified by by fetching the file in the browser:
http://localhost:3000/dev-assets/app.js

Compiled files not getting fingerprint rails

I have a number of environments where I deploy an application. In two of them I deploy the following configuration for assets:
config.assets.enabled = true
config.assets.compress = false
config.assets.compile = true
config.assets.precompile = false
config.assets.digest = true
config.assets.debug = false
config.serve_static_assets = true
My applicaton.js, that compiles a number of .js files on it, is not getting a fingerprint, so I don't serve changes to customers that have already cached it.
The web page then serves all assets precompiled with fingerprint except application.js
Where can I start to look for this failure?
The culprit is the following option :
config.assets.compile = true
It will live compile each file without fingerprint and serve them straight for rails
You should set it to false and use config.assets.precompile which should contain the list of all precompiled filed :
config.assets.precompile += %w(application.js foo.js)
Then don't forget to run rake assets:precompile in your deployment task to recompile new assets.
On a side note, you should also set config.serve_static_assets to false and let nginx / apache / your web server handle it for you, serving assets from rails has a big overhead.

Javascript not loading in Rails Test Environment

I can't get Javascript to load in the browser in the rails test environment. This is problematic for cucumber selenium tests through capybara. Here's my test.rb (the default as far as I know)
MyApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
# Log error messages when you accidentally call methods on nil
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Raise exceptions instead of rendering exception templates
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
end
This results in a concatinated application.js with everything. I've tried adding config.assets.debug = true so that the assets aren't concatinated, and then only sometimes the javascript is loaded. If I copy over development.rb it seems to work, but that doesn't seem ideal.
Thanks!
Turns out it was the less-rails-bootstrap version 3.0.1 gem was loading twitter/bootstrap in the wrong order due to inter-dependencies. We never noticed this is in development because Rails does not compress assets in the development environment, by default. So those particular files weren't being loaded, but they didn't keep all of our javascript from loading like they were in test.
Upgrading to less-rails-bootstrap 3.0.3, or requiring individual components of twitter/bootstrap in the correct order in application.js solved the problem.

Rails Heroku Precompiled Assets Not Loading JS?

I'm setting up my Rails app on Heroku, and running into a problem where all my CSS assets are loaded but none of my JS assets are. I'm using memcached, and followed these instructions: https://devcenter.heroku.com/articles/rack-cache-memcached-rails31.
One thing I noticed is that when I change config.assets.compile to "true" in my production.rb file, all the JS is loaded successfully but obviously the initial load of the site is tremendously slow. How can I troubleshoot this problem? I'm new to Heroku, so am unsure and haven't found anything online that is helpful.
I assume that since all my JS files are in /assets/javascripts/, that the should be included automatically, but it seems as if they're not.
Here's my production.rb:
BrainDb::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
client = Dalli::Client.new
config.action_dispatch.rack_cache = {
:metastore => client,
:entitystore => client,
:allow_reload => false
}
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
config.static_cache_control = "public, max-age=2592000"
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
I also removed everything from public/assets so it would precompile. Here's application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module BrainDb
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.initialize_on_precompile = false
# Enable the asset pipeline
config.assets.enabled = true
config.cache_store = :dalli_store
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
end
UPDATE
Application.js:
//
//= require jquery
//= require jquery_ujs
Add
//= require_tree . at last and precompile
in your application.js.

Categories