Every test needs data. Instead of Rails' global fixtures we use factories that create only the records a test needs, which keeps tests isolated and predictable. Important

...why test order must never change a result. Learn Factories, not fixtures By default Rails uses global fixtures for its tests. This is a giant world of example data that...

...good solution to work around this. When you migrate to managing vendor assets in Rails with Bower, the bower-rails gem comes with its own solution for this problem. It...

...can configure your application to automatically resolve paths before precompiling assets: BowerRails.configure do |bower_rails| # Invokes rake bower:resolve before precompilation. Defaults to false bower_rails.resolve_before_precompile = true

...worldwide by developers looking for help and tips on web development with Ruby on Rails and DevOps. 15 years ago – in 2009 – we wrote our first card. Since then, over...

...in makandra cards We gain the most experience in web development and Ruby on Rails in our day-to-day work, as web development is our largest team and we...

...a proof of concept how a integration (slightly different as the official docs for Rails) might look like in Rails + webpack + Unpoly. Also see the HN discussion for pro and...

In the past we validate and set default values for boolean attributes in Rails and not the database itself. Reasons for this: Older Rails didn't support database defaults when...

An alternative approach, which currently reflects more the general opinion of the Rails upstream on constraints in the database, is adding default values in the schema of the...

...field that is handled by Carrierwave uploaders (or maybe any other attachment solution for Rails) in tests allows different approaches. Here is a short summary of the most common methods...

...RSpec looks for fixture files: RSpec.configure do |config| config.file_fixture_path = "spec/custom_directory" end Alternatives: Rails.root.join('spec/fixtures/files/avatar.jpg').open('r') Rails.root.join('spec/fixtures/files/avatar.jpg').read File.open('spec/fixtures/files/avatar.jpg') (might only work if you run the...

sudo gitlab-rails console Note This takes some minutes to start Send a mail Use the following command on the rails console to send an email.

...becomes complex ▶️ All the Little Things — Sandi Metz refactors the Gilded Rose kata live (RailsConf 2014); watch after your own attempt 📄 Gilded Rose kata — our Ruby version with a test...

On the Rails console, assigning an object to a variable can lead to this strange error (without stacktrace): irb > recipient = Recipient.find(123) Traceback (most recent call last): TypeError (nil can...

...gem install reline, or in projects with Bundler: gem 'reline', '>= 0.2.0' # Fixes TypeError in Rails console

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

By activating strict_loading you force developers to address n+1 queries by preloading all associations used in the index...

Rails offers several methods to manage three types of different cookies along with a session storage for cookies. These are normal, signed and encrypted cookies. By following the happy...

...sparse and only focuses on controller specs, which recommended usage have been limited since Rails 5+ (see "Rails: Support for Rails 5"), this card will summarize some guidance on how...

...insert many records is to have a single INSERT statement describing multiple rows. In Rails 6+ you can do so with ActiveRecord::Base.insert_all. This is very fast, but you...

...chapters 2 "Meaningful Names", 3 "Functions", 4 "Comments" and 17 "Smells and Heuristics" 📘 Growing Rails Applications in Practice — chapters 5 "Dealing with fat models" and 7 "Extracting service objects"

📄 Best practices for writing code comments Video ▶️ All the Little Things — Sandi Metz, RailsConf 2014, 40 min: refactoring a nested conditional into small objects, live Discuss with your mentor...

masilotti.com

...Fixtures are handy for development seed data, they can be loaded in development with: rails db:fixtures:load Downsides Less matchers & library support It is harder to mock with minitest...

postgresql.org

...twentieth in what ordering? The ordering is unknown, unless you specified ORDER BY. In Rails, if you use Record.first or Record.last, it will default to ordering by id.

The change_column method for rails migrations support casting with a custom SQL statement. This allows us to change a column type and keep the former content as the new...

docs.sentry.io

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

makandra dev

Rails 6 includes a WYSIWYG editor, Action Text. It works out of the box quite well, but chances are that you want to add some custom functionality. This card contains...

...some tips how to achieve this. Setup Basically, follow the guide in the Rails documentation. The automated script may not work with the way webpacker is configured in your project...

When using Rails to truncate strings, you may end up with strings that are still too long for their container or are not as long as they could be. You...

} start your server as usual, but go to https://localhost:3000 bundle exec rails s Accept the certificate in your browser See also Creating a self-signed certificate for...

Since Rails 6.1+ you can use .compact_blank or .compact_blank! to remove blank values from collections (e.g. arrays). Remove nil values from an array ['foo', nil].compact...

Remove blank values from collections Array array = [1, "", nil, 2, " ", [], {}, false, true] # Any Rails version array.reject(&:blank?) # => [1, 2, true] # Since Rails 6.1+ array.compact_blank # => [1, 2, true]

...to some path methods generated by your routes. Even though you could technically include Rails.application.routes.url_helpers, this may include way too many methods and even overwrite some class methods in...

...advised to only make the desired methods available: class Project delegate :url_helpers, to: 'Rails.application.routes' def project_path url_helpers.project_path(self) end

position: relative left: -50% float: left .clear clear: both Together with this helper: # Rails 3 def center_float(&block) concat( content_tag(:div, :class => 'center_float_outer_container') do...

...tag(:div, :class => 'center_float', &block) end end + content_tag(:div, :class => 'clear') ) end # Rails 2 def center_float(&block) html = "".html_safe html << content_tag(:div, :class => 'center_float...