...for session hashes. Use update instead. Outline Let's assume you're modifying the Rails session. For simplicity, let's also assume your session is empty when you start (same...
...is the ActionController::Session::AbstractStore::SessionHash class (session is of that type in your Rails application, but also in Rack middleware): class SessionHash < Hash # ... def update(hash) load_for_write...
Warnings like those below may originate from rspec or shoulda-matchers or other gems that have not updated yet to...
Rails 2.3.16+ on Ruby 1.9 causes warnings like this: .../gems/activesupport-2.3.17/lib/active_support/core_ext/string/output_safety.rb:22: warning: regexp match /.../n against to UTF-8 string Many thanks to grosser for supplying a monkey-patch for...
...Rails 2.3 (Commit f93e3f0ec3 fixed it for Rails 3). Just put it into config/initializers/ to make those warnings go away. Since we're using RSpec on most projects, I've...
Simply give the select helper an option :disabled, passing either a single value or an array. You need to specify...
...root_url, :data => { :foo => 'bar', :bam => 'baz' } This will produce: Label Only works in Rails 3. In Rails 2 you do = link_to 'Label', root_url, 'data-foo' => 'bar', 'data...
We're adding a script console-for to open a remote Rails console with one command. Also have a look at shell-for, which this script is relying on.
Ever wondered if there is a reverse for Rails' .each(&:to_s) idiom? Turns out there is... You probably already know, that you can abbreviate code like dogs.each { |dog| dog.bark...
If you want to inspect the compiled code of your erb (or haml) templates, you can run the following code...
...PostgreSQL data types to ActiveRecord and convenient querying extensions for ActiveRecord and Arel for Rails 4.x Common table expressions Relation#with Model.from_cte Arrays Face.where.contains tags: %w[happy smiling...
ActiveRecord has a feature called counter caching where the containing record in a has_many relationship caches the number of its children. E.g. when you have House has...
...many :rooms, Rails can cache the number of rooms in House#rooms_count. Mind that when a model has a column that looks to Rails like a counter-cache column...
...prevent this, test for the right environment and place the JS-code afterwards: - if Rails.env.production? :javascript var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); _gaq.push(['_trackPageview...
Rails comes with a Rake task notes that shows code comments that start with "TODO", "FIXME", or "OPTIMIZE". While it's generally not good practice to leave them in your...
app/helpers/frontend/slider_helper.rb: * [48] [TODO] this is only temporary till frontend is done However, with Rails' default configuration this task will only look through default file extensions, like .rb, .erb, .css...
...be updating some of a form's fields via XHR. You can simply use Rails' fields_for to do things like this in your views (HAML here): - fields_for @user...
January has been a very bad month for Ruby on Rails developers, with two high-severity security bugs permitting remote code execution found in the framework and a separate-but...
...related compromise on rubygems.org, a community resource which virtually all Ruby on Rails developers sit downstream of. Many startups use Ruby on Rails. Other startups don’t but, like the...
The colors in Rails log files are helpful when watching them but, since they are ANSI color codes like ^[[4;36;1m, can be annoying when you are reading the...
When you require the Ruby debugger to be available from your Rails console (e.g. you want to inspect a method's magic), you need to enable it explicitly: script/console --debugger
...is to "vendor" the gem by copying it into the vendor/gems directory of your Rails project. You can then make any changes you require and Rails will use the vendored...
...after a server restart. Unfortunately you need to perform some additional steps to marry Rails and the copied gem. This notes describes what to do. With Bundler
...Using unguessable URLs. This is fast (because Apache can deliver assets without going through Rails), but less secure. When going with the "unguessable URL" approach, it is possible to somewhat...
...with SecureRandom.hex(32), and also put it as url_signature_secret into your secrets.yml. Rails helper To generate an expiring URL, use the following helper: def sign_public_path(path...
view_context.helper_method('args') Rails 2 ApplicationController.helpers.helper_method('args') Also see How to use helper methods inside a model...
...cover in detail how to utilize both views and materialized views within Ruby on Rails, and we can even take a look at creating and modifying them with database migrations...
This has to be added to the VirtualHost configuration for Apache for Rails 2 applications: RailsEnv development When running Rails 3, you need RackEnv development
There is a bug in Rails 3's dbconsole script, which makes the following command open a database console for the development environment: rails dbconsole -p test
...write this instead: rails dbconsole test -p
To allow HTTP 304 responses, Rails offers the fresh_when method for controllers. The most common way is to pass an ActiveRecord instance or scope, and fresh_when will set...
The problem It might seem simple enough to just say: updated_at = Rails.cache.fetch('updated_at_of_expensive_scope') fresh_when last_modified: updated_at The example above calls...
...notes # => collects TODO, FIXME and other Tags from comments and displays them rake about # (Rails 3+) => Rails, Ruby, Rake, Rack etc. versions, used middlewares, root dir, etc...