Sunday, October 16, 2011

Ruby On Rails 3.1 Cheat Sheet

* To create a new application use the rails new command. Replace MyApp with the actual name of your application.

rails new MyApp

* If running Ruby On Rails 3.1 on Ubuntu, you will encounter an error stating " Could not find a JavaScript runtime". To fix this locate the Gemfile in the root of your application and append the following two lines

gem 'execjs'
gem 'therubyracer'

then, to update the bundles run the following command
bundle install

* To run the development webserver for your application use the following command
rails server

Then from any web browser, navigate to port 3000 of your Ruby on Rails server. In my case the machine name is testror. Substitute with your server name or ip address.

* To manually work with the sqlite database, use the sqlite3 command followed by the name of the database file
sqlite3 development.sqlite3

To show a list of tables while inside sqlite use the .tables command

To exit sqlite use the .exit command

* To search for the first record which matches in a database table bound to a model use
@info = Info.find(:first, :conditions => [ "name = ?", "a"])

* Variable Scoping
$ - a global variable
@ - an instance variable
[a-z] - local variable
[A-Z] - constant
@@ - class static variable