Ruby on Rails
TipSheetForBeginners (Version #40)

Things I didn’t get from the tutorials. And really needed to know (or remember, or have to look up every ten seconds).

Getting Started

To create a new rails website:

rails newrailsappname

SQLite is the recommended database for development, but not for production. To create a new rails 2.0 SQLite website:

rails -d sqlite3 newrailsdbname

To create a new rails 2.0 MySql website:

rails newrailsappname -d mysql newrailsdbname

To create your controllers and models:
<pre> ruby script/generate model user ruby script/generate controller admin

ruby script/generate without any options gives you a list of things it can generate, and ruby script/generate _type_ gives you further help.

Beginning

  • Table (schema) in database ( plural, with underscores instead of spaces between words, like steering_wheels)
  • Model ( singular, first letter Capitalized, CamelCase for models like SteeringWheel)
    • Validation of data is here
    • Relationships between various tables
      • Model files have information in them which appears like belongs_to and has_many and reflects automatic connections made by rails, using fields like author_id in a story table
      • belongs_to – a Story model belongs_to an Author. When a table is linked to another table with belongs_to, you can refer to fields in that table by appending the relationship name to the class name. E.g., if product belongs_to category, then in a view that deals with product you can get the category name with product.category.name.
      • has_many – an Author has_many Stories
    • Plurality matters. Be very careful — Rails does stuff with making this singular and plural again, so make sure you are including the appropriate number of s
  • Controller (convention is to use the plural form of the model name when applicable, e.g. stories controller, authors controller)
    • Says what to do and where to do it. Has logic like “if post is not valid, send error about validation”
    • Prepares variable for view from model
      • Create variable of tables using things like the find(:all) command in the API
  • View
    • How things look – ruby in html => .html.erb
    • Uses variables in Controller and prints them
      • Get variable from controller with @ symbol in front of variable ( Ex. @movies in bob_controller action submit => allows submit.rhtml to access something called @movies )
    • <%= debug(variableName) %>@ shows a nicely formatted variable in a rhtml file, or --- if the variable is not there.
    • The scaffolding creates edit, show, and delete links in list views. To replace the text ‘show’ with another field in the list view, do this (the h() thing escapes any html in the name):

      <%= hname = h(product.name) link_to(hname, :action => 'show', :id => product) %>
  • In Rails 2.x scaffolds are much simpler. But they do not work at all like Rails 1.x and trying to follow tutorials based on the older flavor won’t work well. To do it in Rails 2.0 change into the directory that you created with the rails command, then: script/generate scaffold Guide camera:binary obscura:binary title:string. This will automagically do quite a bit:
  • create the model (named Guides) for a database with three fields (named camera, obscura and title)
  • create the corresponding controller and view
  • make the CRUD to do simple database tasks
    But first you have to do a couple of more things:
  • create a database named guides_development with a username and password
  • edit the config/database.yml to set username and password
  • run the rake db:migrate command to create two tables in the database (one for the data and the other for version control)
  • run the script/server to get the program running

Intermediate

  • Sessions sometimes need to be deleted from /tmp/ if everything starts making 404s!
  • If you’re using the DB to store sessions, try rake db:sessions:clear

General

  • Looking for a text editor to use with Rails? If you’re using OS X, you already know about TextMate. For Windows users, try E Text Editor, an excellent TextMate clone. For more text editors see Editors
  • Try using RailsBrain in lieu of the official documentation. It’s much easier to search with all of its AJAX goodness.

Translations

Things I didn’t get from the tutorials. And really needed to know (or remember, or have to look up every ten seconds).

Getting Started

To create a new rails website:

rails newrailsappname

SQLite is the recommended database for development, but not for production. To create a new rails 2.0 SQLite website:

rails -d sqlite3 newrailsdbname

To create a new rails 2.0 MySql website:

rails newrailsappname -d mysql newrailsdbname

To create your controllers and models:
<pre> ruby script/generate model user ruby script/generate controller admin

ruby script/generate without any options gives you a list of things it can generate, and ruby script/generate _type_ gives you further help.

Beginning

  • Table (schema) in database ( plural, with underscores instead of spaces between words, like steering_wheels)
  • Model ( singular, first letter Capitalized, CamelCase for models like SteeringWheel)
    • Validation of data is here
    • Relationships between various tables
      • Model files have information in them which appears like belongs_to and has_many and reflects automatic connections made by rails, using fields like author_id in a story table
      • belongs_to – a Story model belongs_to an Author. When a table is linked to another table with belongs_to, you can refer to fields in that table by appending the relationship name to the class name. E.g., if product belongs_to category, then in a view that deals with product you can get the category name with product.category.name.
      • has_many – an Author has_many Stories
    • Plurality matters. Be very careful — Rails does stuff with making this singular and plural again, so make sure you are including the appropriate number of s
  • Controller (convention is to use the plural form of the model name when applicable, e.g. stories controller, authors controller)
    • Says what to do and where to do it. Has logic like “if post is not valid, send error about validation”
    • Prepares variable for view from model
      • Create variable of tables using things like the find(:all) command in the API
  • View
    • How things look – ruby in html => .html.erb
    • Uses variables in Controller and prints them
      • Get variable from controller with @ symbol in front of variable ( Ex. @movies in bob_controller action submit => allows submit.rhtml to access something called @movies )
    • <%= debug(variableName) %>@ shows a nicely formatted variable in a rhtml file, or --- if the variable is not there.
    • The scaffolding creates edit, show, and delete links in list views. To replace the text ‘show’ with another field in the list view, do this (the h() thing escapes any html in the name):

      <%= hname = h(product.name) link_to(hname, :action => 'show', :id => product) %>
  • In Rails 2.x scaffolds are much simpler. But they do not work at all like Rails 1.x and trying to follow tutorials based on the older flavor won’t work well. To do it in Rails 2.0 change into the directory that you created with the rails command, then: script/generate scaffold Guide camera:binary obscura:binary title:string. This will automagically do quite a bit:
  • create the model (named Guides) for a database with three fields (named camera, obscura and title)
  • create the corresponding controller and view
  • make the CRUD to do simple database tasks
    But first you have to do a couple of more things:
  • create a database named guides_development with a username and password
  • edit the config/database.yml to set username and password
  • run the rake db:migrate command to create two tables in the database (one for the data and the other for version control)
  • run the script/server to get the program running

Intermediate

  • Sessions sometimes need to be deleted from /tmp/ if everything starts making 404s!
  • If you’re using the DB to store sessions, try rake db:sessions:clear

General

  • Looking for a text editor to use with Rails? If you’re using OS X, you already know about TextMate. For Windows users, try E Text Editor, an excellent TextMate clone. For more text editors see Editors
  • Try using RailsBrain in lieu of the official documentation. It’s much easier to search with all of its AJAX goodness.

Translations