There are multiple ways to redirect URLs to a different URL in Rails, and they differ in small but important nuances. Imagine you want to redirect the following url...

tl;dr Prefer request specs over end-to-end tests (Capybara) to joyfully test file downloads! Why? Testing file downloads

github.com

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...

Concurrent counter increments are a race-condition trap. The Rails idiom (load the record, increment in Ruby, save) breaks under concurrent writes. Between the read and the write, another transaction...

makandra dev

...after_logout idp_sign_out] end Unsafe redirect when trying to log out Since Rails 7 you need to pass allow_other_host: true to redirect_to to allow a...

It is very common to parse dates from strings. It seems obvious to use Date.parse for this job. However this...

postgresql.org

...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...

...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...

Running rails server will start a local server that you can access via http://localhost:3000. When you are working on multiple web apps, they will likely set cookies with...

...define it in a single place so switching it out will be easy. Modern rails versions will block hosts other than localhost by default. Therefore create an entry in config/environments/development.rb...

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...

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.

api.rubyonrails.org

...is included in migrations by default (also see How to write complex migrations in Rails). Handy methods Note that both keys and values are always strings, i.e. you'll need...

...so that a tcp connection is used instead of a socket connection. Configure rails application to use the database development: adapter: mysql2 database: projectname_development encoding: utf8mb4 collation: utf8mb4_unicode...

Modern IRB has time measurement built in. measure # Enable measure :off # Disable Custom Should your version of IRB not offer...

...groups are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them. I recommend storing them like this: spec/models/shared_examples/foo.rb spec/models/shared_examples/bar.rb...

...those shared examples available to all specs, put the following into your spec_helper.rb (for rails 4 in rails_helper.rb), above the RSpec.configure block: Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].each {|f| require f...

reinteractive.com

...to configure omniauth-multi-provider to support multiple SAML identity providers for a single Rails app: To solve this, the omniauth-multi-provider gem acts as a dynamic wrapper around...

TL;DR: Pannellum is a small (~56 kB, own WebGL renderer, no three.js) equirectangular panorama viewer. The npm package has...

...you thought it would be, you don't understand how XSS protection works in Rails. Calling html_safe on the joined array will incorrectly bless the complete string as safe...

...string].join(' ').html_safe # will incorrectly render as ' foo bar ' with unescaped tags Good Rails >=3 safe_join([unsafe_string, safe_string], ' ') # will correctly render as '<span>foo...

...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...

...registered constants and the file references during the boot. Therefore you need to add Rails.autoloaders.log! at the end of your config/application.rb file. You could also run bin/rails zeitwerk:check for...

makandra dev

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

makandra dev

$ ruby -e "RubyVM::YJIT.enable; puts RubyVM::YJIT.enabled?" true The last variant is what Rails 7.2+ is doing after the boot process with its new default configuration. So if you...

...are on a current Rails version and didn't explicitly turn off YJIT, you are probably already using it. Keep in mind that Rails turns on YJIT after the boot...

github.com

...All you need is a pretty print-stylesheet. How to use it from your Rails application You can have PDFKit render a website by simply calling PDFKit.new('http://google.com').to...

...separately before calling to_file. Alternatively you can use PDFKit::Middleware and all your Rails routes automagically respond to the .pdf format. This is awesome to get started fast, but...

In Capistrano 3, your Capfile requires 'capistrano/rails/migrations', which brings two Capistrano tasks: deploy:migrate and deploy:migrating. The former checks...