Ever needed to use a global variable in Rails? Ugh, that's the worst. If you need global state, you've probably reached for Thread.current. When you're using Thread.current...
This page lists many query methods and options in ActiveRecord which do not sanitize raw SQL arguments and are not...
You can report CSP violations to Sentry. Within config/initializers/content_security_policy.rb: Rails.application.configure do config.content_security_policy do |policy| # Settings for the policy policy.report_uri 'https://ooo4444bbb.ingest.de.sentry.io/api/ooo4444bbb/security/?sentry_key=ooo4444bbb' end end Replace the actual...
Rails' fragment caching caches subtrees of an HTML document tree. While constructing that tree though, it can be really hard to keep track of whether some code is run in...
...a caching context. Fortunately, Rails 7 brings two helpers that simplify this. Note that these helpers are all about Rails' fragment caching and not about downstream caching (i.e. Cache-Control...
...do it, and it's great, especially in combination with Sprockets (or Propshaft on Rails 7). You might be missing some convenience features, though. Here we cover one specific issue...
...Once you have started your development Rails server and esbuild with the --watch option (if you used jsbundling-rails to set up, you probably use bin/dev), esbuild will recompile your...
When internationalizing your Rails app, you'll be replacing strings like 'Please enter your name' with t('.name_prompt'). You will be adding keys to your config/locales/*.yml files over...
...term, you can use PostgreSQL’s trigram similarity search. Writing a fuzzy query in Rails User.where("similarity(name, ?) > 0.3", "John") This finds all users where the name is similar to...
When putting phone numbers into web pages, you should use tel: links so smartphone users can click those numbers to...
Just found out about a great feature in Rails that seems to be around since Rails 2. Start a console with the --sandbox (or -s) parameter: rails console --sandbox
In the past we validate and set default values for boolean attributes in Rails and not the database itself. Reasons for this: Older Rails didn't support database defaults when...
An alternative approach, which currently reflects more the general opinion of the Rails upstream on constraints in the database, is adding default values in the schema of the...
...in any structure. While its flexibility is great, there is no syntactic sugar in Rails yet. Thus, you need to manually query the database. Demo # Given a Task model with...
...good solution to work around this. When you migrate to managing vendor assets in Rails with Bower, the bower-rails gem comes with its own solution for this problem. It...
...can configure your application to automatically resolve paths before precompiling assets: BowerRails.configure do |bower_rails| # Invokes rake bower:resolve before precompilation. Defaults to false bower_rails.resolve_before_precompile = true
ActiveModel supplies an errors object that behaves similar to a Hash. It can be used to add errors to a...
So you want to organize your I18n using multiple .yml files but your Rails 4.1 application simply won't use any extra files in development? Spring is to blame.
SQL end Usage example: insert_record 'users', name: 'joe', age: 15 Also see Rails: Talking to the database without instantiating ActiveRecord objects...
...a proof of concept how a integration (slightly different as the official docs for Rails) might look like in Rails + webpack + Unpoly. Also see the HN discussion for pro and...
...gem with strategy :transaction, after_commit callbacks will not be fired in your tests. Rails 5+ Rails 5 has a fix for this issue and no further action is needed...
...Rails 3, Rails 4 Add the gem test_after_commit to your test group in the Gemfile and you are done. You don't need to change the database strategy...
On the Rails console, assigning an object to a variable can lead to this strange error (without stacktrace): irb > recipient = Recipient.find(123) Traceback (most recent call last): TypeError (nil can...
...gem install reline, or in projects with Bundler: gem 'reline', '>= 0.2.0' # Fixes TypeError in Rails console
sudo gitlab-rails console Note This takes some minutes to start Send a mail Use the following command on the rails console to send an email.
To reverse lookup a fixture by its table name and id, use the following approach on ActiveRecord::FixtureSet: table = 'users...
You can use constraints in your routes.rb to avoid getting ActionView::MissingTemplate errors when wrong routes are called. Instead, the...
...in our tests for sequential test execution but not for parallel test execution. And Rails requires you to set the config.cache_classes = false if you are using Spring in tests...
...With our setup, this would raise the following error in cucumber-rails for parallel test executions due to some legacy database cleaner issue. WARNING: You have set Rails' config.cache_classes...
Let's say you have a form that you render a few times but you would like to customize your...
This card describes how to install some tasks only for a given Rails environment or for a given Capistrano stage ("deployment target"). Installing jobs only for a given...
...Rails environment In your schedule.rb you may use environment variable to access the Rails environment of the current deployment: if environment == 'staging' every :day do # Setup job that will only...