odie’s webmemory

Entries categorized as ‘rubyonrails’

Checklist for a new Rails project

January 7, 2007 · Leave a Comment

Here’s a cheat sheet to starting a new rails project.

  1. Create the rails proj (rails proj_name)
  2. Update the database connection (config\database.yml)
  3. Create the database (create database)
  4. Generate the model (ruby script\generate model)
  5. Edit the migration file (db\migrate\…)
  6. Create database table for the model (db\migrate\rake migrate)
  7. Generate the scaffold (ruby script\generate model controller)
  8. Run the server (ruby script\server)
  9. Run the app (http://localhost:3000/controller)
  10. Eat the cake

Categories: rubyonrails

Controllers and layouts

December 12, 2006 · Leave a Comment

Every controller has its corresponding layout with the same name. To have a consistent look in an application, every controller should use the same layout. To do this, in the controller you can set the layout by using the following line:

layout "<global_layout>"

Categories: rubyonrails

Generate a model then a scaffold

December 6, 2006 · Leave a Comment

To start a rails project, I would suggest generating a model, then generating a scaffold.

% ruby script/generate model
– creates a migration file; edit the migration file to create the db table for your model
– % rake migrate
—- to create the db table using the migration file

% ruby script/generate scaffold
– creates the layout and the scaffold stylesheet

Categories: rubyonrails

Eclipse, RDT and RadRails

November 20, 2006 · 1 Comment

I am tired of having multiple DOS windows and wordpad editors open all at the same time to work on my ruby on rails project. So I decided to download the Eclipse SDK, and the RDT (RubyEclipse Development) and RadRails plug-ins.

Installation:
1. Download Eclipse SDK and unzip into a directory.
2. Run Eclipse and use the update manager to install the plug-ins.
a. Go to Help -> Software Updates -> Find and Install …
b. Add the sites for the RDT and RadRails plug-ins (as listed in RadRails Update Sites)
i. RDT: http://updatesite.rubypeople.org/release
ii. RadRails: http://radrails.sourceforge.net/update

To use (with existing project):
1. Run Eclipse
2. Set the workspace to the parent directory of the Ruby on Rails project
3. Create a project file
a. File -> New -> Project -> Rails Project
b. Set the project name to the Ruby on Rails project directory

Links:
Eclipse SDK, http://www.eclipse.org/downloads
RDT – Ruby Eclipse Development, http://rubyeclipse.mktec.com/cgi-bin/trac.py/wiki
RadRails, http://www.radrails.org

Categories: rubyonrails

Format the datetime using StrFTime

November 17, 2006 · 1 Comment

To format a datetime, use StrFTime. For example to output the date in the following format “11/16/2006 11:58PM”, do the following: nowDT.strftime(“%m/%d/%y %I:%M%p”). nowDT is your datetime variable.

See the php documentation for further information. This also works for ruby on rails.

Categories: rubyonrails

Generate a model and a controller vs a scaffold

November 14, 2006 · 1 Comment

What do you get when you generate a scaffold instead of generating the model and the controller individually? Below are the excerpts (i.e., edited results) when I ran the generators with the -p option (pretend).

ruby script/generate model entry -p
create app/models/entry.rb
create db/migrate/001_create_entries.rb

ruby script/generate controller entry -p
create app/controller/entry_controller.rb

ruby script/generate scaffold entry entry -p
create app/models/entry.rb
create app/views/entry/_form.rhtml
create app/views/entry/list.rhtml
create app/views/entry/show.rhtml
create app/views/entry/new.rhtml
create app/views/entry/edit.rhtml
create app/controllers/entry_controller.rb
create app/views/layouts/entry.rhtml
create public/stylesheets/scaffold.css

Notes:

  1. The model generator creates a migration file.
  2. The scaffold generator creates actions, views, a layout and a stylesheet.

Categories: rubyonrails