Rails wraps your parameters into an interface called StrongParameters. In most cases, your form submits your data in a nested structure which goes hand in hand with the strong parameters...
...However, there are cases where this does not fit your use case, or has side effects. If you do it differently, be extra careful not to introduce security issues.
...not be observable by non-jQuery code. This is currently a WONTFIX for jQuery (see closed issues #2476, #3347). Native events always work for everyone Note that if you trigger...
...listeners using jQuery's trigger(). Note that you might use third-party libraries like select2 that use trigger(). If your entire app is written in jQuery and you don't...
...Adding Records via XHR and JS Example For the following examples we use a simple data model where a user has zero or more tasks. class ExampleMigration < ActiveRecord::Migration...
...user.tasks.build } end def update load_user @user.attributes = user_params if @user.save flash[:notice] = 'User saved successfully.' redirect_to(edit_variant_1_user_path(@user)) else flash[:notice] = 'User could not...
A JavaScript error in an E2E test with Selenium will not cause your test to fail. This may cause you to miss errors in your frontend code. Using the BrowserConsole...
!!driver_logs_proc end def driver_logs_proc browser = page.driver.browser if browser.respond_to?(:logs) # selenium-webdriver >= 4 proc { browser.logs } elsif browser.respond_to?(:manage) && browser.manage.respond_to?(:logs) # selenium-webdriver...
...Plotting graphs in Ruby with Gruff, which comes handy for many uses cases, you sometimes might need configuration for more advanced plots, e.g. for academic concerns. Then using Gnuplot, the...
...first academic open source plotting software, might be a good option. There are several wrappers for Ruby available and I mainly looked at one of the two most frequently used...
...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[:id]) fresh_when @user
...by passing an array of ETaggable objects to fresh_when. class UsersController < ApplicationController def show @user = User.find(params[:id]) # The show template also renders the user's posts. fresh_when...
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...
...generic names on localhost. This is annoying, since you will sign out your current user whenever you switch to another app. A better way is to use our own daho.im...
You can hook into Slack when using Capistrano for deployment. The slackistrano gem does most of the heavy lifting for you. Its default messages are unobtrusive and can be adjusted...
When deploying, it posts to a Slack channel like this: How to integrate Integrating Slackistrano with Capistrano 3 is fairly simple. If you are not a Slack admin
...Object into Integer (TypeError) Integer(2) # 2 Integer("11", 2) # 3 This is very similar but not identical to to_i: "2".to_i # 2 "foo".to_i # 0
...an instance of Object (NoMethodError) 2.to_i # 2 "11".to_i(2) # 3 Integer() supports a exception: false variant, which is very handy to cast user input without any exception...
...table, two things happen: Rails tries to load all involved records in a huge single query spanning multiple database tables. The preloaded association list is filtered by the where condition...
...you only wanted to use the where condition to filter the containing model. The second case's behavior is mostly unexpected, because pre-loaded associations usually don't care about...
The nokogiri gem provides different packages for several platforms. Each platform-specific variant ships pre-built binaries of libxml2, e.g. x86_64-linux includes binaries for 64bit Linux on Intel/AMD...
...This significantly speeds up installation of the gem, as Nokogiri no longer needs to compile libxml2. However, this also means that for each security issue with libxml2, Nokogiri maintainers have...
...command-line tool for REPL (Read Eval Print Loop). Type gem help info to see a list of available switches. Note that the Ruby API offers a bit more insight...
...Gem Ruby API (at runtime) First, get the Gem::Specification of the gem you are looking for. That object holds all the information from the gem's gemspec file, and...
...is a gem for working with daytimes. That's a tuple of (hour, minute second) without a day, month or year. Another additional gem? Thus SQL has a time datatype...
...for storing time of day in the format hh:mm:ss, neither Ruby nor Rails themselves offer an elegant way to deal with day times. Time and DateTime both handle...
...of a Linux process, like so: $ faketime 'last Friday 4 pm' date Fr 12. Sep 16:00:00 CEST 2025 However, we cannot just modify the time of the docker...
...need to affect the command being run inside the image. So we need to smuggle faketime into the container somehow, and activate it for the process being run.
Static error pages To add a few basic styles to the default error pages in Rails, just edit the default templates in public, e.g. public/404.html. A limitation to these default...
...templates is that they're just static files. You cannot use Haml, Rails helpers or your application layout here. If you need Rails to render your error pages, you need...
...that matter) will not be affected by this. If you define them in your specs, they will exist globally. This is because of how RSpec works (short story: instance_eval...
# ... end let(:record) { TestRecord.new }
end # TestRecord will exist here, outside of the spec! Do not do this. It will bite you eventually. For example, when you try to...
You have uncommited changes (you can always check by using git status), which you want to discard. Context Now there are several options to discard these depending on...
...your exact situation. The headlines will differentiate the cases whether the files are staged or unstaged. Staged and unstaged changes Staged changes Unstaged Changes Staged and unstaged changes
...in licensing, we cannot provide Elasticsearch versions >= 8.0. Version 7.17.x will reach EOL status with the release of Elasticsearch version 9. We have decided to use OpenSearch as a...
...replacement, since it is a fork of Elasticsearch version 7.10.2, still running under the previous licensing model and wire-compatible. A more detailed reasoning can be found on their website...
To make CSS rules dependent on the screen size, we use media queries: @media (max-width: 500px) { // rules for screen widths of 500px or smaller } Browsers will automatically enable and...
...disable the conditional rules as the screen width changes. To detect responsive breakpoints from JavaScript, you may use the global matchMedia() function. It is supported in all browsers:
...always is to prevent long-running queries in the first place, automatic timeouts can serve as a safety net to terminate problematic queries automatically if a set time limit is...
...statement due to statement timeout (PG::QueryCanceled)). If multiple SQL statements appear in a single simple-query message, the timeout is applied to each statement separately. Set the timeout globally...
I frequently find myself needing a combination of group_by, count and sort for quick statistics. Here's a method on Enumerable that combines the three: module Enumerable
group_by(&block) .transform_values(&:count) .sort_by(&:last) .to_h end end Just paste that snippet into a Rails console and use #count_by now! Usage examples...
When you find similar groups of expect calls in your tests, you can improve readability by extracting the group into its own matcher. RSpec makes this easy by allowing matchers...
We can extract the repeated matcher chains into a custom matcher called be_shouting: expect(foo).to be_shouting expect(bar).to be_shouting Instead of re-implementing the...
...for DOM elements, there are some footguns you should know about. Some lists are synchronized with the DOM Some DOM APIs return live lists that automagically update their contents as...
...reflects the change automatically: document.querySelector('#two').remove() console.log(liveList) // [#one] console.log(nonLiveList) // [#one, #two] Snapshotting a live list Lists that silently change their elements can be very surprising to work...
When ending a Selenium test Capybara resets the browser state by closing the tab, clearing cookies, localStorage, etc. It may be a good idea to wait for all in-flight...
...AJAX requests to finish before ending a scenario: You may have client-side JavaScript that freaks out when the tab closure kills their pending requests. If that JavaScript opens an...