Notes posted by mcmire
RSS feedMoved to ActiveRecord::Persistence
This isn’t deprecated, it was just moved in Rails 3. See:
Moved to ActionDispatch::Routing::PolymorphicRoutes
In Rails 3, this has moved to ActionDispatch::Routing::PolymorphicRoutes.
Re: Caveat when using dynamic layouts
Since there’s no way to edit posts on here, I need to correct myself and say that what I posted before doesn’t work, since you can’t specify layout multiple times:
class OrdersController < BaseController layout :determine_layout, :only => :new layout "public", :except => :new # ... end
So don’t do that. The only way to ensure that the other actions get the default theme is to drop :only/:except and do the conditions yourself:
class OrdersController < BaseController layout :determine_layout private def determine_layout %w(new).include?(action_name) ? "some_layout" : "public" end end
All this to say, beware of :only/:except – they aren’t as useful as you think they are.
Caveat when using dynamic layouts
Worth noting that if you have a controller which inherits from another controller which has a layout, and in this child controller you’re determining the layout at runtime using a method for specific actions, the other actions you are excluding will not inherit the layout from the parent controller.
For example, if you’ve got this
class BaseController < ApplicationController layout "public" end class OrdersController < BaseController layout :determine_layout, :only => :new # index, show, new, create, edit, update, destroy ... end
then OrdersController#index, #show, and #edit won’t get the “public” layout – in fact they won’t get a layout at all. So you’ll need to do this instead:
class OrdersController < BaseController layout :determine_layout, :only => :new layout "public", :except => :new # ... end
Includes descendants
May be helpful to know that this returns true if A is any descendant of B, not just a direct one. As an example:
class Foo; end class Bar < Foo; end class Baz < Bar; end Bar < Foo #=> true Baz < Foo #=> true
If you want direct descendance try Class#superclass:
Bar.superclass == Foo #=> true Baz.superclass == Foo #=> false
Don't forget to require 'tmpdir'
If you simply say Dir.tmpdir you might get a nice surprise:
irb> Dir.tmpdir NoMethodError: undefined method `tmpdir' for Dir:Class
from (irb):1
Strangely, this method seems to be stored in a file that Ruby doesn’t require by default. Just require 'tmpdir' and all should be well.
irb> require 'tmpdir' => true irb> Dir.tmpdir => "/var/folders/An/AnwlXPZFH2aRLCERERQDKE+++TI/-Tmp-"
Overriding default validation messages
Before Rails 2.2 you could globally customize the default validation error messages by changing AR::Base.default_error_messages. The messages have now been moved to i18n, so to customize them in 2.2 and up, just create a locales/ folder in your config/ folder, copy activerecord/lib/active_record/locale/en.yml (in Rails source) to config/locales/en.yml, and then change the strings inside. As szeryf indicated below, the strings of interest are activerecord.errors.messages.
Conditions work for lower-level validate methods too
I don’t think this is mentioned in the docs anywhere, or else I couldn’t find it: Because validate, validate_on_create, and validate_on_update are ActiveSupport::Callbacks, their symbol forms support conditions just like validates_presence_of and company:
validate :permaname_must_be_unique, :if => :normal_entry? validate_on_create :posted_at_must_be_valid_timestamp, :unless => Proc.new {|e| e.posted_at.nil? } validate_on_update :title_must_not_contain_apostrophes, :if => :title_starts_with_a_b?
Superclass of OrderedHash
Note that in Rails 2.3, OrderedHash changed from being a subclass of Array to a subclass of Hash. This is contrary to what the documentation says above.
Gotcha with method calls inside select loop
Keep in mind that any methods you call on the object in the select loop will be strung together when the Javascript is rendered. For example:
page.select(".shipping_type_fee").each do |td| td.down("span").update("--").show td.down("img").hide end
will be rendered as:
$$('.shipping_type_fee').each(function(value, index) { value.down("span").update("--").show().down("img").hide(); });
This is probably not what you want!
Re: Using a Loading Graphic
You probably want to be using :complete, not :loaded, to execute Javascript when an Ajax request has finished. See: http://prototypejs.org/api/ajax/options
List of status codes and their symbols
Note that the :status option accepts not only an HTTP status code (such as 500), but also a symbol representing that code (such as :created), if that makes more sense to you. Here’s a list of which symbols map to which numbers (derived from ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE):
100 = :continue 101 = :switching_protocols 102 = :processing 200 = :ok 201 = :created 202 = :accepted 203 = :non_authoritative_information 204 = :no_content 205 = :reset_content 206 = :partial_content 207 = :multi_status 226 = :im_used 300 = :multiple_choices 301 = :moved_permanently 302 = :found 303 = :see_other 304 = :not_modified 305 = :use_proxy 307 = :temporary_redirect 400 = :bad_request 401 = :unauthorized 402 = :payment_required 403 = :forbidden 404 = :not_found 405 = :method_not_allowed 406 = :not_acceptable 407 = :proxy_authentication_required 408 = :request_timeout 409 = :conflict 410 = :gone 411 = :length_required 412 = :precondition_failed 413 = :request_entity_too_large 414 = :request_uri_too_long 415 = :unsupported_media_type 416 = :requested_range_not_satisfiable 417 = :expectation_failed 422 = :unprocessable_entity 423 = :locked 424 = :failed_dependency 426 = :upgrade_required 500 = :internal_server_error 501 = :not_implemented 502 = :bad_gateway 503 = :service_unavailable 504 = :gateway_timeout 505 = :http_version_not_supported 507 = :insufficient_storage 510 = :not_extended