Notes posted by ronald

RSS feed
January 21, 2013 - (v3.0.0 - v3.0.9)
0 thanks

for rails >= 3

template_root= is deprecated, use prepend_view_path instead

source: ActionMailer/DeprecatedApi/ClassMethods/template_root

May 20, 2011 - (>= v3.0.0)
0 thanks

relative_url_root= no longer exists in Rails 3.0

Rails 3.0 does this with scope in config/routes.rb

scope "/exampleapp" do
  resources :examples
  root :to => "examples#index"
March 16, 2010 - (>= v2.2.1)
0 thanks



ActionController::AbstractRequest.relative_url_root= "/exampleapp"


config.action_controller.relative_url_root= "/exampleapp"
June 10, 2009
0 thanks

list of predefined variables

$! The exception information message set by ‘raise’. $@ Array of backtrace of the last exception thrown.

$& The string matched by the last successful pattern match in this scope. $` The string to the left of the last successful match. $‘ The string to the right of the last successful match. $+ The last bracket matched by the last successful match. $1 to $9 The Nth group of the last successful regexp match. $~ The information about the last match in the current scope.

$= The flag for case insensitive, nil by default. $/ The input record separator, newline by default. $\ The output record separator for the print and IO#write. Default is nil. $, The output field separator for the print and Array#join. $; The default separator for String#split.

$. The current input line number of the last file that was read. $< The virtual concatenation file of the files given on command line. $> The default output for print, printf. $stdout by default. $_ The last input line of string by gets or readline.

$0 Contains the name of the script being executed. May be assignable. $* Command line arguments given for the script sans args. $$ The process number of the Ruby running this script. $? The status of the last executed child process. $: Load path for scripts and binary modules by load or require.

$“ The array contains the module names loaded by require. $DEBUG The status of the -d switch. $FILENAME Current input file from $<. Same as $<.filename. $LOAD_PATH The alias to the $:. $stderr The current standard error output. $stdin The current standard input. $stdout The current standard output. $VERBOSE The verbose flag, which is set by the -v switch. $-0 The alias to $/. $-a True if option -a (”autosplit“ mode) is set. Read-only variable. $-d The alias to $DEBUG. $-F The alias to $;. $-i If in-place-edit mode is set, this variable holds the extension, otherwise nil. $-I The alias to $:. $-l True if option -l is set (”line-ending processing“ is on). Read-only variable. $-p True if option -p is set (”loop“ mode is on). Read-only variable. $-v The alias to $VERBOSE. $-w True if option -w is set.

Source: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants#Pre-defined_Variables

June 4, 2009
2 thanks


Ruby style

Dir[File.join(RAILS_ROOT, 'vendor', 'plugins', '*')]

Rails style

Dir[Rails.root.join('vendor', 'plugins', '*')]
March 27, 2009
4 thanks

multiple filter example

actually you can have it even shorter with:

before_filter :authorize, :set_locale, :except => :login
March 26, 2009
1 thank
March 25, 2009 - (>= v2.2.1)
1 thank

usage helper with block

helper example

def my_helper(&block)
  inner = capture(&block)
  out = "<somehtml>#{inner}</somehtml>"
  block_called_from_erb?(block) ? concat(out) : out

view example

<% my_helper do %>
<% end %>



using ActionView::Helpers::CaptureHelper#capture

March 25, 2009 - (>= v2.2.1)
1 thank


not sure but block_called_from_erb? seems to be the replacement

March 20, 2009 - (v2.2.1)
0 thanks
March 20, 2009 - (>= v2.3.2)
1 thank
February 4, 2009
0 thanks
February 4, 2009
0 thanks

usage example


class MyModel < ActiveRecord::Base
  def created_today?
    self.created_on.to_date == Date.today
February 2, 2009
1 thank



@items = Item.find(:all)
@items.should have_at_least(1).item
January 26, 2009
5 thanks

two ways to disable single table inheritance

  1. Don’t use the column name ‘type’

  2. Or if the first is no option for you: Tell Rails to look for a not existing column like:

class MyModel < ActiveRecord::Base

  # disable STI
  inheritance_column = :_type_disabled
January 26, 2009 - (>= v2.2.1)
3 thanks

alternative (working with 2.2.X)