Warning: Constant FS_METHOD already defined in /var/www/defrails.com/wp-config.php on line 95
Misc | def rails Misc | def rails

Category Archives: Misc

Misc

Rails 3 Cheat Sheet

Here are some quick tips for common Rails tasks.

Getting Started

To create a new application use the command:

rails new APP_NAME

Remember that if you want to create a template for building a Rails app, you can pass the rails new command a template using the -m or –template option.  To quickly create a template, be sure to check out the RailsWizard.

After you create an application, you’ll most likely want to test the result by starting the built-in server (WEBrick).

rails server

For those lazy programmers out there, you can just use rails s.

Generators

Rails has many built-in generators.  You can see the entire list by running the rails generate command without any options.  Remember that in Rails 3 you can use various shortcuts for commands.  With rails generate for example, you could just use rails g instead.  Here are a few common generators (the files created or changed are bolded in each example):

Scaffold

Generates a migration, a model, a controller, 5 views (index, show, edit, new, _form), a helper, unit test for the model, a fixture for the model, a functional test for the controller, a unit test helper, a stylesheet (scaffold.css), and finally it will add a resource route.

rails generate scaffold RESOURCE_NAME [field:type field:type] [options]

Scaffolding will get you up and running quickly, but it doesn’t always fit your particular applications.  Experienced developers typically choose to either avoid the scaffolding generator entirely or override the functionality with their own templates.

Model

Generates a migration, a model, a unit test, and a fixture for the model

rails generate model MODEL_NAME [field:type field:type] [options]

Migration

Generates a migration for changing the database.

rails generate migration NAME [field:type field:type] [options]

Remember, if you give Rails a smart name, it will generate the correct code for you.  If your migration is in the form of add_xxxxx_to_yyyyy or remove_xxxxx_from_yyyyy and is followed by a list of columns and types, then the appropriate add_column and remove_column calls will be added automatically for you. You can also specify the name using UpperCamelCase such as AddXxxxToYyyy or RemoveXxxxFromYyyyy.

Controller

Generates a controller, functional test, a helper, and views for each of the actions you specify.

rails generate controller NAME [action action] [options]

If you use the controller generator and it is going to host a collection of resources, make sure your controller name is plural (e.g. notes, books, etc.).

Mailer

A Rails mailer is similar to a controller but is used for sending out email.  It generates the mailer class, a functional test for the mailer, a fixture for testing, and views for each of the mails/methods that you specify.

rails generate mailer NAME [method method] [options]

 

Migrations

Many of the generators create migrations for you, but you can build your own.  A migration is just a Ruby class designed to alter your database.  It consists of two methods, self.up to migrate your database, and self.down to rollback the migration.

By generating a migration, the generator is only creating a file, not altering the database structure.

To migrate your database (run all the pending migrations), use the command rake db:migrate.  This will run the self.up method within a migration.

To rollback a migration, use the command rake db:rollback.  This command will run the self.down method within a migration.

Bundler

The best way to utilize gems in a project is by using Bundler.  Bundler works on a simple principle: you have a Gemfile within your project where you specify the gems that are required for your application/component.  In most cases (like within Rails), this will handle requiring that gem for you.  After you specify a gem, you need to tell Bundler that you made a change.  You can do this by issuing the command:

bundle install

By running a bundle install, Bundler will go out to whatever source you have specified in your Gemfile and will install any gems you do not have.  It also takes a snapshot of the gems and versions each time you run a bundle install and puts the information into the Gemfile.lock file.  By doing this, Bundler can quickly check if you have the appropriate gems on your system.  In addition, the Gemfile.lock is very helpful when deploying your application because it contains exact information about the required gems.

Useful Rake Tasks

We already looked at two rake tasks in the Migrations section above, rake db:migrate and rake db:rollback.

If you want to see the routes you have in your application, you can use the command rake routes and it will list the name of the route, the URI, the controller it will be routing to, and the specific action of the controller.

You can see all of the rake tasks available to you by issuing the rake -T command.

Misc

Useful Links

For more information on coding in Rails, its many uses, and  other items of interest, check out the following sites:

Visoft, Inc. Sites

def rails – The resources for Visoft’s Rails courses (this site).

Visoft, Inc. Home – The Visoft, Inc home page.

Visoft, Inc. Blogs – Various articles covering .NET, C#, Ruby, and Rails.

Developer’s Bookcase – The inspiration for the Bookcase application from the Rails 101 class.  A place for developers to log all of their books.

EageReader – A clone of devbookcase.com for general book lovers.

Installs

Rails Installer – Easy installation for installing Ruby and Rails on Windows.

Ruby Installer – Ruby installation packages for Windows. If you just want Ruby without the rest of the Rails Installer package, you can get it here.

Editors and IDEs

TextMate – Fantastic editor, but only available for Macs.  It utilizes TextMate bundles to add syntax highlighting, macros, and additional functionality.

e-TextEditor – Fantastic editor for Windows.  Similar to TextMate on the Mac and can use TextMate Bundles (referred to as tmbundles).

Aptana Studio – An IDE (Integrated Development Environment) that supports Ruby on Rails.

RubyMine – A rich IDE for Ruby and Rails development that has a similar feel to Visual Studio with ReSharper (R#) installed.

Books

Agile Web Development with Rails (4th edition) – Fantastic book that covers Rails 3, contains a tutorial, and a deep dive into the framework.

The Rails 3 Way – Great reference book covering just about everything in Rails.

Docs and More

RubyOnRails.org – The homepage for Ruby on Rails.

Rails API Documentation – All of the classes and methods found in Rails.

RubyGems.org – The majority of gems are hosted here.  If you want to find out about a gem’s dependencies or more information about a gem (like it’s homepage, wiki, etc.), rubygems.org is the one stop shop.

GitHub – Repository for the majority of gems that are open-source.

LDAP and Ruby – StackOverflow post on using a gem to query Active Directory.

Railscasts – Excellent videos on a plethora of Rails topics.

RailsWizard.org – Want to create “rails new” template that includes all of the things you’ll be using in the application up-front?  The RailsWizard will create a Ruby script for you that will do just that.

Ruby Toolbox – Their tagline “know your options” sums it up best.  There are tons of gems out there, but which should you choose?  The Ruby Toolbox ranks gems by various criteria (GitHub watchers, forks, last checkin, etc.) in different categories.

jQuery

jQuery.comjQuery is “the” library for assisting you in writing JavaScript.  It simplifies many tasks such as event handling, animation, and Ajax interactions.

jQueryUI – A library of add-ons for jQuery.  It quickly adds functionality like drag and drop, calendar controls, effects, etc.

jQuery Mobile – Touch-optimized web framework for smartphones and tablets.

jQuery Tools – Contains fantastic controls that add-on to the base jQuery library.  Be sure to check out the various demos.  You can either download the scripts or have them hosted for you on a CDN (Content Delivery Network).

Google’s CDN – Google hosts many JavaScript frameworks (including jQuery, jQueryUI, Prototype, etc) on their CDN.

Hosting Rails

Heroku – One of the easiest ways to deploy and host a Rails application.

Phusion Passenger – Also known as mod_rails, it is a server capable of running Rails applications on top of Apache or nginx.  You can also run Passenger in standalone mode.

HTML5 Examples

Basecamp Mobile – A mobile version of 37signals BaseCamp application.  Be sure to also read the initial announcement where the question “why a web app and not a native app?” is answered.

The Financial Times App – An HTML5 mobile application targeted at iOS devices like the iPhone or iPad.