Rails has always included a scaffold script that generates a default controller implementation for you. Unfortunately that generated controller is unnecessarily verbose. When we take over Rails projects from other...

...We prefer a different approach. We believe that among all the classes in a Rails project, controllers are some of the hardest to test and understand and should receive special...

When deleting a record in your Rails app, Carrierwave automatically takes care of removing all associated files. However, the file's container directory will not be removed automatically. If you...

dependent: :restrict_with_error and dependent: :destroy both install before_destroy callbacks, and Rails fires them in declaration order. The restricting associations are declared first, so their checks run...

...works when the restricting association can be declared first. A has_many :through cannot: Rails raises ActiveRecord::HasManyThroughOrderError unless the through association is declared after the association it rides on...

...the behavior, not the implementation" means. Resources Chapter "The value of tests" from Growing Rails Applications in Practice (in our library) Everyday Rails Testing with RSpec (in our library)

...Movie.search() and the new UI. Follow the advice from the "Testing" chapter from Growing Rails Applications in Practice: RSpec feature tests for the most common "happy path" RSpec unit tests...

I have a form with a dynamic number of fields. Submitting it worked fine until I tried out a very...

github.com

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

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

...can specify your preferred version like so: bundle _2.1.2_ update --bundler Older Ruby and Rails cannot use the latest bundler 2 version, so you need to stay on bundler...

makandra dev

Getting CSS (and JS) live reloading to work in a esbuild / Rails project is a bit of a hassle, but the following seems to work decently well. We assume that...

...you already use a standard "esbuild in Rails" setup, and have an esbuild watcher running that picks up your source code in app/assets and compiles to public/assets; if not change...

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

...setups. This can become pretty messy. The trick: spin up throwaway controllers with rspec-rails' anonymous controller helper, drive real requests, and classify the response. 2. Simplest possible case

...SomeController isn't sufficient here. It isolates routes. A bare routes.draw mutates the global Rails.application.routes. The helper swaps in a fresh RouteSet and restores it afterwards. It names the subclass...

api.rubyonrails.org

ActiveSupport (since 4.1) includes test helpers to manipulate time, just like the Timecop gem: To freeze the current time, use...

...have to either: Sort both the query and database content. If you're on Rails 7.1 you can use the new normalizes macro for this. This solution would still use...

Background information about session storage in Rails Rails has a default mechanism to store the session in the CookieStore. This is a cookie which holds the entire user session hash...

I recently stumbled upon the Rails feature composed_of. One of our applications dealt with a lot of addresses and they were implemented as 7 separate columns in the DB...

...enforced that. Because I used a regular class, I had to build it myself. The Rails-native readonly is sadly only available with ActiveRecord, not with ActiveModel. It would have...

Understand how nested attributes appear in the params. See how the Rails form helpers encode the names of nested inputs. Understand how the record and all of its nested...

...saved in a transaction. That means the entire structure is saved or not. Resources Rails Guide: Nested forms Nested Forms in Rails Popular mistakes when using nested forms When aggregating...

api.rubyonrails.org

ActiveModel supplies an errors object that behaves similar to a Hash. It can be used to add errors to a...

greg.molnar.io

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

If you want someone to be able to access your rails console, but don't want them to be able to do changes you can use the rails console sandbox...

...invoking bin/rails console --sandbox. https://guides.rubyonrails.org/command_line.html#bin-rails-console To let one only access the sandbox rails console you can make use of the command option of OpenSSH: Specifies that the command...

...Nested example groups before(:each) after(:each) let subject RSpec.configure, config.before, config.after Resources Everyday Rails Testing with RSpec (in our library), chapter 8 (Keeping Specs DRY) Note: Please refer to...

...render_template() matcher that helps with test above. To get this matcher, add a gem rails-controller-testing. Tip If you place your spec file in spec/requests you don't...

apidock.com

All columns of a model's database table are automagically available through accessors on the Active Record object.

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

...using ActiveStorage's disk service. This means that stored files are served by your Rails application, and every request to a file results in (at least!) one non-trivial log...

...an example of what loading a single in an example application writes to the Rails log. Started GET "/rails/active_storage/blobs/redirect/..." for ::1 at ... Processing by ActiveStorage::Blobs::RedirectController#show as SVG...

By default, Rails' validates_uniqueness_of does not consider "username" and "USERNAME" to be a collision. If you use MySQL this will lead to issues, since string comparisons are case...

...may fail with an SQL error due to duplicate index key. You can change Rails' behaviour, by saying class User < ActiveRecord::Base validates_uniqueness_of :name, case_sensitive: false