Ruby on Rails
TipSheetForBeginners
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 newrailsappname
To create a new rails 2.0 MySQL website:
rails -D mysql newrailsappname
To create your controllers and models:
ruby script/generate model [model]
ruby script/generate controller [controller] [action]
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
- 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 runningIntermediate
- 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
Updated
on October 13, 2008 06:02
by
Conrad (142.151.154.206)