LoadError: Could not load the ‘listen’ gem (Rails 5)

If you are on rails 5 and you are using the default config/environments/development.rb file it will have this line of code in there. config.file_watcher = ActiveSupport::EventedFileUpdateChecker This requires the gem listen. This threw me for a bit as I was doing a rails 4 upgrades to a rails 5 edit: Forgot to mention that if … Read more

What to use instead of `render :text` (and `render nothing: true`) in rails 5.1 and later?

The non-deprecated way is to use render :plain Rails Guide on Layouts and Rendering: 2.2.6 Rendering Text You can send plain text – with no markup at all – back to the browser by using the :plain option to render: render plain: “OK” Bonus Instead of render nothing: true (also removed), one should now use … Read more

What are the brackets [5.1] after ActiveRecord Migration and how does it work? [duplicate]

This is the new migration versioning introduced with Rails 5. The number indicates the migration version the migration was created with, in this case version 5.1 and should be used with Rails versions >= 5.0. This is a class function def self.[](version) of the ActiveRecord::Migration, which calls Compatibility.find(version) and is used for backward compatibility. Here … Read more

rescue_from ActionController::RoutingError in Rails 4

In application_controller.rb add the following: # You want to get exceptions in development, but not in production. unless Rails.application.config.consider_all_requests_local rescue_from ActionController::RoutingError, with: -> { render_404 } end def render_404 respond_to do |format| format.html { render template: ‘errors/not_found’, status: 404 } format.all { render nothing: true, status: 404 } end end I usually also rescue following … Read more

ActiveRecord OR query Hash notation

There are 5 options that could be considered as implementations of «Hash notation» (the last two are kinda hash-ish): With Ruby on Rails 5 you are able to do the following chaining using ActiveRecord::Relation#or method: Person.where(name: ‘Neil’).or(Person.where(age: 27)) Use where_values together with reduce. The unscoped method is necessary only for Rails 4.1+ to ensure default_scope … Read more

tech