...object lists constants, ancestors with their methods, variables etc. Adding console helper methods # config/initializers/console.rb Rails.application.console do def custom_helper ... end end Generating paths app.root_path Making requests app.host = 'localhost'
...html_safe and translate them with = t('.text_html'). When you're localizing a Rails application, sometimes there is this urge to include a little HTML. Be it some localized...
...to learn more about <em>the corporation</em>. Alright. Rails is being helpful here and saves you from accidentally injecting HTML into the page. But how...
When your controller action raises an unhandled exception, Rails will look at the exception's class and choose an appropriate HTTP status code and error page for the response.
...instance, an ActiveRecord::RecordNotFound will cause Rails to render a red "The page you were looking for doesn't exist" with a status code of "404" (not found).
...to summarize by example the different uses of heredoc. In Ruby << vs. <<- vs. <<~ In Rails strip_heredoc vs. squish strip_heredoc should be used for a text, where you want...
Using heredoc for prettier Ruby code How to: Ruby heredoc without interpolation Rails 3+ def foo bar = <<-TEXT.strip_heredoc line1 line2 line3 TEXT puts bar.inspect end
To reverse lookup a fixture by its table name and id, use the following approach on ActiveRecord::FixtureSet: table = 'users...
...card describes different flavors for concatting HTML safe strings in a helper method in Rails. You might want to use the tag helper instead of the content_tag helper (the...
When upgrading Rails versions -- especially major versions -- you will run into a lot of unique issues, depending on the exact version, and depending on your app. However, it is still...
...to tackle the update in principle. If you are not really confident about upgrading Rails, have a look at Rails LTS. How many update steps? Besides the Rails upgrade itself...
...a good default to add a unique index on the foreign key when using Rails’ has_one relationship. This ensures the database enforces the 1:1 constraint and raises an...
...I needed to do to add esbuild to an application that used the vanilla rails asset pipeline with sprockets before. Preparations update Sprockets to version 4 add a .nvmrc with...
...your preferred node version (and install it) add gems jsbundling-rails and foreman to your Gemfile: gem 'jsbundling-rails' group :development, :test do gem 'foreman' # ... end bundle install
Normally, Rails handles encryption and signing of cookies, and you don't have to deal with the matter. Should you need to decrypt a session cookie manually: here is how...
...Obviously, you can only decrypt a session cookie from within the corresponding Rails application. Only the Rails application that encrypted a cookie has the secrets to decrypt it.
...to your DB schema. Ignore the column With ignored_columns you can prevent your Rails models from generating accessor methods for these columns: class User < ApplicationRecord self.ignored_columns += [:legacy_field...
#pluck is commonly used as a performant way to retain single database values from an ActiveRecord::Relation Book.pluck(:title, :price...
Sometimes you need to remove high Unicode characters from a string, so all characters have a code point between 0...
...module structure. The typical example would be the concerns folder, which exists in new Rails applications by default and does not create a constant module Concerns. app ├── models ├── concerns ├── shareable.rb...
...following official api: app ├── models ├── shared ├── shareable.rb # Defines constant Shared::Shareable # e.g. in application.rb Rails.autoloaders.main.collapse("#{Rails.root}/app/models/shared") # with collapsed shared dir app ├── models ├── shared ├── shareable.rb # Defines constant Shareable
Starting with Rails 7.1 the production logger is set to standard out. For applications running with opscomplete ensure to keep logging to a file as before (e.g. when running bin/rails...
...be enough to change these lines in the config/environments/production.rb back to the implementation in Rails <7.1: - # Log to STDOUT by default - config.logger = ActiveSupport::Logger.new(STDOUT) - .tap { |logger| logger.formatter = ::Logger::Formatter.new
...B.end && B.start <= A.end The code below shows how to implement this in Ruby on Rails. The example is a class Interval, which has two attributes #start_date and #end_date...
Rails 7.1 added a new method Rails.env.local?. If you want to stub the Rails env correctly, use ActiveSupport::EnvironmentInquirer like this: # check if the value of stubbed_env is valid...
...allow(Rails).to receive(:env).and_return(ActiveSupport::EnvironmentInquirer.new(stubbed_env.to_s...
Rails supports time zones, but there are several pitfalls. Most importantly because Time.now and Time.current are completely different things and code from gems might use one or the other.
...only about one time zone is a bit tricky. The following was tested on Rails 5.1 but should apply to Rails 4.2 as well. Using only local time
All columns of a model's database table are automagically available through accessors on the Active Record object.
...written as a Ruby Gem and was tested and evaluated against one Ruby on Rails project. This card will summarize and present the research results, the evaluation and the programmed...
...The tool should be suitable for modern web based development workflows with Ruby on Rails. Criteria Faster results of failing tests than random prioritization to lower regression testing costs.
Short reference on how to quickly debug the vanilla Rails job adapters. Queue Adapters by Environment Environment Adapter Jobs Run In Worker Needed? development :async Rails server process No
...Scripts and rake tasks log go to log/development.log Watch out for this within the Rails or development logs: Enqueued HelloJob (Job ID: xxx) to Async(asap) Performing HelloJob (Job ID...
Rails has generic error messages you can define in your locale .yml files. You may override those application-wide error messages using model or attribute scope like this: en: activerecord...
In Rails 7.2 the new default for config.action_dispatch.show_exceptions is rescuable. :rescuable: It will show a Rails error page in the response only for rescuable exceptions as defined by ActionDispatch...
stack trace and a good debugging experience. :all: It will show a Rails error page in the response for all exceptions (previously true) :none: It will raise for...
tl;dr Prefer request specs over end-to-end tests (Capybara) to joyfully test file downloads! Why? Testing file downloads