Basic 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...
Occasionally you need to do something directly on the server -- like having all records recalculate something that cannot be done in a migration because it takes a long time.
...s say you do something like this: Project.all.each(&:recalculate_statistics!) Even though you may have been successful with this on your development machine or the staging server, keep in mind...
Line-height and vertical-align are simple CSS properties. So simple that most of us are convinced to fully understand how they work and how to use them. But it...
...less-known feature of CSS: inline formatting context. For example, line-height can be set as a length or a unitless value 1, but the default is normal. OK, but...
...moving a file, your imports also need to change. To get around this, esbuild support a mechanism first introduced in TypeScript called "path aliases". It works like this:
...if you do use TypeScript): { "compilerOptions": { "baseUrl": "./app/assets", "paths": { "@/*": ["js/*"], "@images/*": ["images/*"], "@css/*": ["css/*"], "@spec/*": ["../../spec/js/*"] } }, "include": [] } (The include: [] only makes sense if you do not use Typescript. If you...
expect(page).to have_field('Username') { |field| field[:class].blank? } Limitations Using execute_script, evaluate_script or evaluate_async_script within a filter block will crash with a timeout...
...Selenium::WebDriver::Error::ScriptTimeoutError Exception: script timeout This is due to Capybara using a zero timeout while calling the filter block. A workaround is to temporarily set a different timeout...
The attached patch lets you find a record by a string or number in any column: User.find_by_anything('carla') User.find_by_anything('email@domain.de') User.find_by_anything(10023)
User.find_by_anything!('carla') Boolean and binary columns are excluded from the search because that would be crazy. I recommend copying the attachment to features/support/find_by_anything.rb, since it is...
}) Example case I had an Unpoly compiler for a tab navigation, which sets an -active class per default to the first tab link and removes it from all...
...other tab links. If another tab link is clicked, the -active class switches to the clicked link. In a Jasmine spec I wanted to test this behaviour. Unpoly's up.hello...
...any of the gems that GEM depends on. For the example above you would say: bundle update cucumber-rails --conservative If Bundler complains of a single dependency that it cannot...
...resolve this way, simply add it to the command: bundle update cucumber-rails capybara --conservative Options for old versions of bundler The options below might be relevant if you're...
...animate HTML elements using the Web Animation API's method .animate(keyframes, options) (which seems to be Baseline for all browsers since 2022). const fadeIn = [{ opacity: 0 }, { opacity: 1 }] // this...
// the animation object can be used for things like querying timings or state or `pause`, `cancel` the animation animation.pause() animation.play() await animation.finished // do other stuff Even though play...
To return non-HTML responses (like XLS spreadsheets), we usually use the respond_to do |format| format.xls do # send spreadsheet end end This is often, but not always the same...
...one format checks for authorization and the other doesn't. params[:format] is only set when a user explicitly puts a .xls at the end of the URL. The format.xls...
...or ::1 (IPv6) can only be reached from your own PC: Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN...
...local address 0.0.0.0 can be reached from other PCs on your network: Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
Looking at the source code of the validates_numericality_of validator, it becomes clear that it converts the attribute in question to either an integer or float: if configuration[:only...
...any of your users as this is only broken in the most extreme case shown above...
...want some output. In Rails > 3.1 you have two methods at hand: announce and say_with_time. In the migration: class AddUserToken < ActiveRecord::Migration class User < ActiveRecod::Base; end
add_column :users, :token, :string announce "now generating tokens" User.find_in_batches do |users| say_with_time "For users ##{users.first.id} to ##{users.last.id}" do users.each do |user| user.update_attribute :token...
When a user shares your content, a snippet with title, image, link and description appears in her timeline. By default social networks will use the window title, the first image...
...the current URL and some random text snippet for this purpose. This is often not what you want. Luckily Facebook, Twitter, etc. lets you control how your content appears in...
...can use ETags to allow clients to use cached responses, if your application would send the same contents as before. Besides what "actually" defines your response's contents, your application...
...probably also considers "global" conditions, like which user is signed in: class ApplicationController < ActionController::Base etag { current_user&.id } etag { current_user&.updated_at } end Under the hood, Rails generates...
...want to be in there. In order to reduce the chance to accidentally commit something you didn't intend, review your changes before committing. My preferred way of doing this...
...N . # Add all paths, but not their contents git add -p Git will now show you all your changes in small chunks and ask you in an interactive mode whether...
PostgreSQL's Common Table Expressions (CTEs) can be used to extract sub-queries from bulky SQL statements into a temporary table to be referenced instead. This is most useful to...
...avoid ugly joins or sub-selects. CTEs can be used for SELECT, INSERT, UPDATE or DELETE. Example (from the PostgreSQL docs): WITH regional_sales AS ( SELECT region, SUM(amount) AS...
...with information from an associated table, you can JOIN the associated table into the statement. Example Let's say you have a database schema where an Employee belongs_to :department...
Now you need to backfill existing Employee records with the new department_name. Since the department's name lives in another table, you need to JOIN both tables during...
$number = $length / 1px // => 13 Converting a unit the result of an addition or subtraction between two numbers of different units is expressed in the first member’s unit
...the desired unit: $duration: .21s $duration-in-milliseconds: 0ms + $duration // => 210ms An example is storing a transition duration as CSS custom property to read it from Javascript. By converting the...
Sometimes it's nice to have some coloring in your logs for better readability. You can output your logs via tail and pipe this through sed to add ANSI color...
...containing "FATAL" in red and all lines with "INFO" in green: tail -f /path/to/log | sed --unbuffered -e 's/\(.*INFO.*\)/\o033[32m\1\o033[39m/' -e 's/\(.*FATAL.*\)/\o033[31m...
...you prepare your changelist properly, it directs your reviewer’s attention to areas that support your growth rather than boring style violations. When you demonstrate an appreciation for constructive criticism...
...your reviewer provides better feedback . Make others better: Your code review techniques set an example for your colleagues. Effective author practices rub off on your teammates, which makes your job...
The benefit of the Rails asset pipeline is that it compiles your stylesheets and javascripts to a single file, respectively. However, the consequences are startling if you don't understand...
...all your asset libraries in the same folder, which quickly becomes confusing as your set of assets grows. To overcome this, we have two different solutions. Custom solution
...default order" of rows in database tables. For instance, when you paginate a result set: When using LIMIT, it is important to use an ORDER BY clause that constrains the...
...result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows. You might be asking for the tenth through twentieth rows, but tenth...
...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! This will come bite you at least when you try to define a class with...