Rails: Cucumber tests (with JS) failing after adding assets to CSS - ruby-on-rails

I'm pretty new to rails so this is a bit of mystery to me.
I've added some font files to app/assets/fonts and included them in a SCSS file using asset-url helper. However I'm now getting errors similar to:
No route matches assets/9df317a3-a79e-422e-b4e2-35ccd29cd5b7 (ActionController::RoutingError)
(note the missing file extension?)
on my Cucumber tests, but only on tests with the #javascript flag. I've tried the following:
RAILS_ENV=test rake assets:precompile
and the fixes in this thread didn't work:
Capybara tests with :js=>true... Routing Error: No route matches [GET] "/assets"
The thread suggests that there is an incorrect asset somewhere, but this happens even if I remove all but one file from my CSS. Plus the app reports no 404s and the fonts are working in the dev env. These are the only assets in the app!
I'm using:
Rails 4
Cucumber
Capybara
Poltergeist (for JS tests)
Css:
#font-face{
font-family:"l baskerville w01_n4";
src:asset-url("8dc59876-75a4-4e80-bd1a-735d5f043beb.eot?#iefix")format("eot")
}
etc...
(the files are provided by fonts.com [hence the horrible filenames])
environments/test.rb:
Rails.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
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure static file server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'
# 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
# Randomize the order test cases are executed.
config.active_support.test_order = :random
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Any help would be appreciated, I'm guessing this is an issue with the asset pipeline in a test and production environment. But I don't know where to start.
Thanks,
LM

Turns out the answer is a simple syntax error:
src: asset-url("8dc59876-75a4-4e80-bd1a-735d5f043beb.eot?#iefix")format("eot")
Should be:
src: font-url(url("8dc59876-75a4-4e80-bd1a-735d5f043beb.eot?#iefix")) format("eot")
Hopefully this is useful to some other newbies like me.

Related

Restart Rails server automatically after every change in controllers

It should not be necessary to restart Rails server after any normal change. However, when I make little changes on my app controllers, they aren't applied if I don't restart the server. Even if I wrote bad code and made errors intentionally, the old error persists. How can I change that or verify that's well set up?
I have in config/environment/development.rb file:
config.cache_classes = false
This didn't work for me.
UDPATE 1:
development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# 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
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
You can use nodemon for auto restarting your old rails applications.
1) Install nodemon.
sudo npm install -g nodemon
2) Create nodemon.json file on root dir and configure it
{
"ignore": [
".git",
"node_modules/**/node_modules"
],
"watch": [
"app/controllers/",
"app/models/",
"app/assets/",
"config/",
"db/"
],
"ext": "rb yml js css scss"
}
3) Create rails.sh file on root dir to start rails application using nodemon.
kill -9 `cat tmp/pids/server.pid`
echo "APP READY!!!"
echo "Ruby on Rails"
rails s -d
4) Give permission to rails.sh file.
sudo chmod +x rails.sh
5) Start server using sh command.
nodemon -L --exec "./rails.sh"
Note: Steps verified on mac machine. different os may have different command or configuration.

Rails does not reload controllers, helpers on each request in FreeBSD 9.1

I've detected weird behavior of rails. Please give me some advice!
For example I have a code like this:
def new
raise
end
I start rails server in development mode.
Hit refresh in browser and see
RuntimeError in AuthenticationController#new
Okay. I comment out line with "raise" like this:
def
# raise
end
Hit refresh in browser but again I see that error as shown above. Even though in browser I see code with commented out "raise".
My guess is that controllers and helpers etc. are getting reloaded but rails returns cached results.
config/environments/development.rb:
Rails.application.configure do
# BetterErrors::Middleware.allow_ip! '192.168.78.0/16'
# 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
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = false
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
How I start server:
=> Booting Puma
=> Rails 4.2.1.rc3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.11.1 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3000
Any suggestions please.
UPDATE 1.
This problem does not exists in Ubuntu 14.04 but exists in FreeBSD 9.1.
I've created simple app and tested it out in FreeBSD first (same problem), in Ubuntu then (no problem).
Can you help me with advice how to deal with this problem on FreeBSD 9.1?
Had the same issue with Rails 5 + Vagrant + Ubuntu 16. None of the other solutions worked (my guest and host times are synced).
The only thing that worked for me was to comment out the following line from config/environments/development.rb:
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
Thought I would post this in case someone else gets to this page for a similar issue, as I did.
I have finally figured this out!
Here's an answer on rails tracker: https://github.com/rails/rails/issues/16678
If you use VirtualBox + NFS you must synchronize time between host and client due some changes in Rails 4.
Please check if you are really running the application in development mode, rather than production.
Also check you /config/environments/development.rb to see if cache classes is off:
config.cache_classes = false
This other post might help you.
Rails.application.reloader.reload!
found with method(:reload!).source in rails console.
(rails 6)

Rails assets are cached even in Development

Here is my Development.rb file
# 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 caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.assets.digest = true
config.serve_static_assets = false
For some reason even in the development environment my CSS files are being cached and I can't figure out why
Was having similar issue: Javascript files were cached by the rails server
Solution (as mentioned by TheIrishGuy):
Stop Rails Server
$rake tmp:cache:clear
Start Rails Server
Crtl+F5 in the browser
Did you precompile any assets? If so check the public assets folder and remove.
Try clearing /tmp/cache/assets
Make sure the browser isn't caching the application.css, force refresh etc

Rails compiles assets both with and without md5 hash, why?

I'm relatively new to RoR and I'm curious about why Rails compiles assets both with and without md5 hash for production?
I run bundle exec rake assets:clean then bundle exec rake assets:precompile
My production.rb file:
MyApp::Application.configure do
# 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 = false
# 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.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
config.assets.precompile += %w(tos.js, tos.css)
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
My application works with files with hashes in their names and it's the way it should be in my case :)
So I have two questions here:
1) Why is it happening when compiled?
Rails compiles assets both with and without md5 hash for production
2) What are these files (without hashes) for?
Maybe I don't get something, so please could someone explain.
The reason it does it is so that you can access the files without knowing the MD5 fingerprint (for example in a non-rails application, or a file within the rails app which isn't compiled or run by the rails stack (e.g. a 500/502 status error page). In this case you would have to compile the assets then change the css/js links in the static HTML files each time you updated the code (thus causing a change in the MD5 hash).
So instead rails produces 2 copies of each asset file, one with the fingerprint in the filename, the other without (e.g. application-731bc240b0e8dbe7f2e6783811d2151a.css, and application.css). The fingerprinted version is obviously preferred (see 'what is fingerprinting and why should I care' in the rails asset pipeline guide). But the non-digested version is there as a fallback.
As a final thought on the matter I'd take a read of the following pull request to the rails git repo: https://github.com/rails/rails/pull/5379 where they are discussing the pros and cons of the non-digested filenames, and the possibility of being able to turn off compilation of them.
HTH

Hiding all exception messages

I'm running Rails 3.0.8 with Webrick webserver started in production mode with such command
RAILS_ENV=production rails server
I have a following problem.
I've read, that rails in production mode should handle all exceptions and errors.
But I'm actually still having error message "ActiveRecord::RecordNotFound" when I'm trying to get unexisted item in production mode.
I've also read about
rescue_from ActiveRecord::RecordNotFound, :with => :page_not_found
such hack, but I think that it isn't a Rails-way.
here's my production.rb file contents:
BeerPub::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# The production environment is meant for finished, "live" apps.
# Code is not reloaded between requests
config.cache_classes = true
config.whiny_nils = false
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_view.debug_rjs = false
config.action_controller.perform_caching = true
# Specifies the header that your server uses for sending files
config.action_dispatch.x_sendfile_header = "X-Sendfile"
# For nginx:
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
# If you have no front-end server that supports something like X-Sendfile,
# just comment this out and Rails will serve the files
# See everything in the log (default is :info)
# config.log_level = :debug
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Disable Rails's static asset server
# In production, Apache or nginx will already do this
config.serve_static_assets = true
# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
As you can see, it is quite usual.
Please, help me to solve this issue.
UPD:
I can also get following error
Routing Error
No route matches "/lol"
It's another type of exception, but the question is the same. What is the Best way to handle such situations?
"I've read, that rails in production mode should handle all exceptions and errors."
This is incorrect, Rails doesn't catch exceptions per-se, but it's just that in production mode you have different ways to handle them.
The rescue_from method you wrote is absolutely correct.
Many Rails developers don't care about rescueing the RecordNotFound exception for the simple fact that, depending on the app, is the user that did something wrong.
There are apps however that likes to trap this exception and perform custom actions, such as redirects, rendering text or different views.

Resources