Rails offers a way to prepend (or append) view paths for the current request. This way, you can make the application use different view templates for just that request.
...action :prepare_views def index # ... end private def prepare_views if prepend_view_path Rails.root.join('app', 'views', 'special') end end end If is true, Rails will first look into app/views/special...
Rails' default logger prefixes each log entry with timestamp and tags (like request ID). For multi-line entries, only the first line is prefixed which can give you a hard...
...time when grepping logs. Example Rails.logger.info(<<~TEXT) Response from example.com: Status: 200 Body: It works! TEXT With that, the following is written to your log file.
...older formatter API. Maybe there will be a fix for that eventually. Update cucumber-rails to >= 1.6.0: bundle update cucumber-rails Upgrade cucumber: bundle update cucumber Make sure you have...
This cards describes an example with a Github Client on how to keep your Rails application more maintainable by extracting domain independent code from the app/models folder to the lib...
...scenarios and not limited to API clients. Example Let's say we have a Rails application that synchronizes its users with the Github API: . └── app └── models ├── user │ ├── github_client.rb │ └── sychronizer.rb └── user.rb...
...need to decide, which configuration between different environment works good for you. By default Rails uses these settings for your application: require(:user) raises in all environments ActionController::ParameterMissing if...
Rails offers the fresh_when method to automatically compute an ETag from the given record, array of records or scope of records: class UsersController < ApplicationController def show @user = User.find(params...
Rails is split into a large number of (sub-) frameworks. The most important and central of those are activesupport (extends the Ruby standard library) activerecord / activemodel (ORM for Rails)
...gem 'actionmailer', require: false) as well. When you look into the definition of rails/all (in railites-x.x.x/lib-/rails/all.rb) you can find the exact path for requiring the sub-frameworks.
By activating strict_loading you force developers to address n+1 queries by preloading all associations used in the index...
...so please feel free to contribute! General workflows The official guide How to upgrade Rails: Workflow advice Upgrading Rails: Example commits Upgrade to Rails 7 Don't use log level...
...debug in your production environments Rails 7.1: Take care of the new production log default to standard out Upgrade to Rails 6 Don't use log level :debug in your...
...creating a database table for a join model without further importance, you can use Rails' create_join_table: class CreateSchoolsStudents < ActiveRecord::Migration[7.2] def change create_join_table :schools, :students...
We are using assignable_values for managing enum values in Rails. Nevertheless Rails is adding more support for enum attributes, allowing to have a closer look at the current feature...
...to our still preferred option assignable_values. Active Record enum attribute interface By default Rails is mapping enum attributes to integers: class Conversation < ActiveRecord::Base enum :status, [ :active, :archived ]
Postgres supports multiple built-in range datatypes: int4range int8range numrange tsrange (range with timestamp without timezone) tstzrange (range with timestamp...
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. Note
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...
After an upgrade to rails 7 I noticed that async reindexing jobs of Searchkick were failing for Model.reindex(mode: :async, wait: true): /home/a_user/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/searchkick-5.3.1/lib/searchkick/relation_indexer.rb:142:in `block in batch_job': undefined...
...with HSTS. The HSTS part is important. Use a reliable authentication solution, e.g. Compose Rails authentication primitives, Clearance or Devise. Don't start from scratch (see bottom).
...root path: xsendfile: unable to find file: /tmp/foo20110721-28050-1h104da-0 The reason for this is that Rails 3 uses X-Sendfile for file downloads and Apache is only allowed to transfer files...
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...
...you most likely want to use this in combination with the ActionDispatch::AssumeSSL middleware (Rails >= 7.1). This middleware makes your app assume that SSL terminates at the load balancer and...
...custom middleware to automatically flag all cookies as secure-only In a Ruby on Rails app you can add a middleware that automatically sets the Secure flag to all server...
As your Rails project grows, you will accumulate a number of small patches. These will usually fix a bug in a gem, or add a method to core classes.
...change_storage.rb fix_cache_ids.rb sanitize_filename_characters.rb ruby/ range/ covers_range.rb array/ dump_to_excel.rb xss_aware_join.rb enumerable/ collect_hash.rb natural_sort.rb string/ to_sort_atoms.rb rails/ find_by_anything.rb form_builder.rb form_for_with_development_errors.rb Note how all patches for standard library classes are in the ruby...
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...
Icon fonts like Font Awesome are infinitely scalable, look great on high-DPI displays and will give your app a...
The :test adapter doesn't respect limits_concurrency configuration. Switch to :solid_queue adapter in your test to verify blocking...
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...