Wed
4
Jan '06
Ruby on Rails cheatsheet
by Frank Spychalski filed under articles, Ruby

If you find this useful, you might like my other articles, too.

A growing list of things I don’t want to forget…


Disclaimer:

This is still very beta! AFAIK there are no errors on this page, but a lot of stuff is still incomplete.


Sections


General

Setup for debian

Install the following packages:

  • ruby – An interpreter of object-oriented scripting language Ruby
  • irb – Interactive Ruby
  • ri – Ruby Interactive reference
  • rdoc – Generate documentation from ruby source files
  • libmysql-ruby – MySQL module for Ruby

For the first few steps I highly recommend toying around with irb, an interactive ruby shell.

Useful gems
  • actionmailer – Service layer for easy email delivery and testing.
  • actionpack – Web-flow and rendering framework putting the VC in MVC.
  • actionwebservice – Web service support for Action Pack.
  • activerecord – Implements the ActiveRecord pattern for ORM.
  • activesupport – Support and utility classes used by the Rails framework.
  • db_structure – [Rails] Database utilities.
  • localization_generator – [Rails] Localization generator.
  • rails – Web-application framework with template engine, control-flow layer, and ORM.
  • rake – Ruby based make-like utility.
  • salted_login_generator – [Rails] Login generator with salted passwords.
Ruby Developer Tools for Eclipse
  • Help->Software Updates->Find and Install
  • “Search for new features to install”, click Next
  • “New Remote Site”
  • name: whatever you like, “Ruby Developer Tools” would be sensible, URL: http://rubyeclipse.sf.net/updatesite, click OK
  • select RDT, click Next
  • select RDT again, click Next
Online documentation
Support


Ruby

attr_accessor :name
attr_reader   :name
attr_writer   :name


Rails

Rails configfiles
  • config/boot.rb: don’t touch
  • config/database.yml: database config for all environment
  • config/environments/(development|production|test).rb: config which differs between environments
  • config/environment.rb: global config
  • config/routes.rb: configure mapping of urls to controllers and actions
Environments

Start server for the different environments:

ruby script/server -e development|test|production
script/generate


Model

Naming conventions
  • Class names: CamelCase, MyEntry
  • Table names: lowercase_underscores_plural, my_entries
finder
User.find(:all, :conditions => ["login != ?", user.login])
table joins

user table with user table in friends table (columns user_id and friend_id)


creates for example for friends the following methods:

  • add_friends
  • friends
  • friends=
  • friends_count
  • has_friends?
  • remove_friends
  • validate_associated_records_for_friends


View

ERb
  • <%= returns value %>
  • <% just execute this %>
  • no newline afterwards if closed with -%>
render
form helpers

size and maxsize are optional parametes for *_field

other helpers

create one / two stylesheet <link>s.


Features

Login


Books

 


Any comments? Or questions? Just leave a Reply: