RICK’S RAILS CHEAT SHEET
Validators validates_presence_of :column_name, :column_name validates_length_of :column_name, :minimum => 1, :maximum => 255, :in => 1..255, :too_short => ‘way too short’, :too_long => ‘duh’ validates_acceptance_of :column_name, :accept => ‘Y’ # accept defaults for cbox validates_confirmation_of :password # password + password_confirmation validates_uniqueness_of :column, :scope => ‘column’ validates_format_of :column, :with => /REGULAR_EXPRESSION/ validates_numericality_of :column, :only_integer => true, :allow_nil => true validates_inclusion_in :column, :in =>
validates_exclusion_of :column, :in => validates_associated :association # validates that the associated object is valid Options for all validators :message => ‘Custom Error Message’ :on => <:create or :update> :if => ... # evaluates an if find Method find() find([, , ]) find :all find :first, :conditions => [ “column = ?”, foo_variable ] Parameters :order => ‘column ASC’ :offset => 20 # starts 20 rows in :limit => 10 # only returns 10 rows :group => ‘column’ # GROUP BY :joins => ‘LEFT JOIN ...’ # JOIN syntax, use include! :include => [ :association_a, :association_b ] # LEFT OUTER JOIN magic :select => [ :col_a, :col_b ] # SELECT these columns :readonly => true # objects are write protected
Some Model Methods Model.average :column Model.minimum :column Model.maximum :column Model.sum :column, :group => :grouping_column
Model Associations belongs_to :model_name :class_name => ‘MyClass’ :foreign_key => ‘my_real_id’ :conditions => ‘column = 0’ has_one :model_name :dependent => :destroy :order => ‘name ASC’ :through => :model_table
ruby script/plugin discover list install install -x install update source unsource sources
ruby script/generate: model ModelName controller ListController show edit scaffold ModelName ControllerName migration AddNewTable plugin PluginName mailer Notification lost_password signup web_service ServiceName api_one api_two integration_test TestName session_migration
# other class name # primary key # “WHERE” # has everything in belongs_to # duh # “ORDER” # joins in data using another table # has everything in has_one + # calls delete without “destroy” method # sets fields to NULL # GROUP BY # SQL statement used to select data # SQL datatement used for counting Callbacks * (-) save * (-) valid? * (1) before_validation * (2) before_validation_on_create * (-) validate * (-) validate_on_create * (4) after_validation * (5) after_validation_on_create * (6) before_save * (7) before_create * (-) create * (8) after_create * (9) after_save
has_many :model_name :dependent => :delete_all :dependent => :nullify :group => ‘name’ :finder_sql => ‘SELECT ....’ :counter_sql => “SELECT ...’
has_and_belongs_to_many :other_model_name Session/Flash/Cookies session[:session_var] = a_var # Sets a session variable flash[:flash_var] = ‘Flash Me!’ # Flash is only available on the NEXT page viewed session :off # turn session management off :only => :an_action :except => :an_action :session_secure => true :if => Some { |ruby| code }
render Method render :action => ‘action_method’, :content_type => ‘super/content-type’ render :partial => ‘name’, # _name.rhtml cookies[:cookie_name] = “Cookie Value” :status => 500 # return HTTP status code 500 cookies.size # returns total number of cookies :locals => { :name => @var } # pass local variables cookies.delete :cookie_name :collection => @list # pass @list is partials name :spacer_template => ‘spacer’ # spacer template AJAX (Remember <%= javascript_include_tag :defaults %>) render :template => ‘path/to/tmp’ # renders a specific template link_to_remote “link”, render :file => ‘/path/to/file’ # render a specific file :update => ‘a_div’, # or { :success => ‘a_div”’ :failure => ‘a_div’ :status => 404, :layout => true :url => { :action => ‘an_action’, :id => object.id }, render :text => ‘Text To Render’ # callbacks ------ :name => ‘javascript(code)’ render :inline => ‘<%= erb code => :here %>’ :loading # when the remote document is loading render :nothing, :status => 404 :loaded # when the document has been loaded Template Quickies :interactive # when the user can interact with the document while loading <%= link_to “Link Text”, :controller => “people”, :action => “show”, :id => @person %> :success # called when successful HTTP status code is returned (2xx range) <%= image_tag “image.png”, :class => “the_class”, :align => “middle” %> :failure # called when failed HTTP status code is returned (not in 2xx range) <%= stylesheet_link_tag “a_css”, “a_directory”, :media => “all” %> :complete # when call is complete, called after success/failure <%= form_tag { :action => :an_action }, { :method => :post } %> :404 # specify a reaction to a specific return code <%= text_field :model, :attribute, options %> form_remote_tag :html => { :action => url_for(:controller => ‘ctrl’, :action => ‘action’, :method ... ) } <%= hidden_field ... %> <%= password_field ... %> <%= file_field ... %> <%= text_field_with_auto_complete :model, :attribute %> <%= text_area :model, :attribute, :cols => 5, :rows => 5 %> # requires auto_complete_for :model, :attribute in controller <%= radio_button :model, :attribute, :tag_value, options %> <%= observe_field (:field, :frequency => 0.5, :update => :a_div, :url => { :action => :an_action }) %> <%= check_box :model, :attribute, options, on_value, off_value %> <%= periodically_call_remote(:update => :a_div, :url => { ... }, :frequency => 2) %> <%= select :model, :attribute, options, html_options %> <%= collection_select :variable, :attribute, options_list, :id, :default_value %> <%= date_select :variable, :attribute, options %> <%= datetime_select :variable, :attribute, options %>