From at least Rails 4, the ActionView tag helper turns Array values of HTML options into a single space-separated string. This means you can pass an array to :class...
Rails 5.2 soft-deprecated the storage of secrets in secrets.yml in favor of a new thing, credentials.yml.enc. Rails 7.1 deprecated secrets and Rails 7.2 finally removed it. In our permissions...
...for existing applications it may be appropriate to keep using secrets.yml. Restoring secrets in Rails 7.2+ Restoring Rails.application.secrets is really simple, thanks to config_for. Simply add this to config/application.rb...
Icon fonts like Font Awesome are infinitely scalable, look great on high-DPI displays and will give your app a...
Since Rails 7 you are able to encrypt database information with Active Record. Using Active Record Encryption will store an attribute as string in the database. And uses JSON for...
...need to configure your Active Record Encryption keys manually in the config/application.rb: config.active_record.encryption.primary_key = Rails.application.secrets.dig(:active_record_encryption, :primary_key) config.active_record.encryption.deterministic_key = Rails.application.secrets.dig(:active_record_encryption, :deterministic_key) config.active_record.encryption.key_derivation...
Rails slightly changed the fragment cache implementation from Rails 7.0 to Rails 7.1. Unfortunately, this is incompatible with how Haml 5 patches Action View buffers. I tried turning a String...
...an ActionView::OutputBuffer, but this brought up other issues. Conclusion While we have a Rails 7.2 application successfully running with Haml 5, Rails applications with fragment caching need to upgrade...
Greg Molnar has written a neat article about creating a single-file Rails app. This is not meant for production use but can be useful to try things out, e.g...
...when hunting down a bug or embedding a Rails app into the tests of a gem. What you do is basically: Put everything (gems, application config, database migrations, models, controllers...
When you want to group rails models of a logical context, namespaces are your friend. However, if you have a lot of classes in the same namespace it might be...
'accounting_' end end class Accounting::Invoice < ApplicationRecord ... end class Accounting::Payment < ApplicationRecord ... end Rails will be able to derive the table name accounting_invoices for Accounting::Invoice...
...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...
...scope :all_tags, -> (tags){ where('tags @> ARRAY[?]', tags) } end Document.create(title: "PostgreSQL", tags: ["pg","rails"]) Document.any_tags('pg') Document.all_tags(['pg', 'rails']) Migration: class CreateDocuments < ActiveRecord::Migration def change
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...
Authentication is a special part of web applications. On the one hand, it usually is a crucial security mechanism restrict...
This page lists many query methods and options in ActiveRecord which do not sanitize raw SQL arguments and are not...
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...
In Rails 7.1 it has become possible to annotate partials with the locals they expect: # partial _user_name.erb <%# locals: (user:) %> <%= user.name %> # view <%= render 'user_name' %> <%# this raises an ArgumentError %> Unfortunately, when...
...remove the annotation to see the correct error. There is an open issue in rails...
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...
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...
...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
In FactoryBot factories, Rails' file_fixture is not available by default. To enable it, include a support module from rspec-rails: FactoryBot::SyntaxRunner.include(RSpec::Rails::FileFixtureSupport) That includes ActiveSupport::Testing...
SQL end Usage example: insert_record 'users', name: 'joe', age: 15 Also see Rails: Talking to the database without instantiating ActiveRecord objects...
...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...
...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
When putting phone numbers into web pages, you should use tel: links so smartphone users can click those numbers to...