#42
Jun 08, 2007

with_options

Several methods in rails take a hash of options as the last argument. If you are passing the same options to several methods, you can remove this duplication by using with_options. Learn all about it in this episode.
Download (9 MB, 3:55)
alternative download for iPod & Apple TV (5.5 MB, 3:55)
# models/user.rb
with_options :if => :should_validate_password? do |user|
  user.validates_presence_of :password
  user.validates_confirmation_of :password
  user.validates_format_of :password, :with => /^[^\s]+$/
end

attr_accessor :updating_password

def should_validate_password?
  updating_password || new_record?
end

# routes.rb
map.with_options :controller => 'sessions' do |sessions|
  sessions.login 'login', :action => 'new'
  sessions.logout 'logout', :action => 'destroy'
end

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source