Warning: Constant FS_METHOD already defined in /var/www/defrails.com/wp-config.php on line 95
def rails def rails
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.

Installation

Installing E-TextEditor and Cygwin

E-TextEditor is a very useful tool in the world of Ruby on Rails.  It combines a robust text editor with syntax highlighting for a large library of languages while also incorporating Cygwin (a UNIX shell for Windows) in order for you to build and execute code directly in the text editor.

E-TextEditor utilizes bundles, which allows for endless possibilities when it comes to support for languages and frameworks.  E is similar to a program called TextMate that runs solely on the Mac, but thankfully programmers can enjoy a comparable experience on Windows. In fact, E-TextEditor runs many of TextMate’s bundles on Windows.  This is all thanks to Cygwin. “Get that Linux feeling – on Windows!” 

Below are the instructions for installing the trial version of E-TextEditor, but purchasing a copy is easy, and you can unlock the full version from within the the trial software.

All of the steps shown below were carried out on a Windows 7 Professional 64-bit system with Service Pack 1 installed.

    1. Download the E-TextEditor Free Trial installation file here: http://www.e-texteditor.com/
    2. Run the installer.
    3. Click “Next” on the initial installation screen.
      Initial Installation
    4. Accept the License Agreement and click “Next.”
      License Agreement
    5. Leave the default installation path as it is and click “Next.”
      Installation Path
    6. Customize the additional tasks you wish the installer to perform. It is recommended to leave the last two boxes checked (“Add ‘Edit with e’ to explorers right click menu” and “Add e command line tool to your system path”).
      Click “Next” when ready to begin the installation.
      Additional Tasks
    7. You will see the installation progress as below:
      Installation Progress
    8. Once the installation of E-TextEditor is complete, you will see the following.  Click “Finish.”
      Installation Complete
    9. Now, open E-TextEditor from the Start menu.
    10. Upon the first run of E-TextEditor you will get the following dialog about installing Cygwin:
      Install Cygwin Dialog
    11. Ensure that “Automatic” is checked and that you have an active internet connection, then click “OK.”
    12. The Cygwin installation process is a bit lengthy. During this you will see a window similar to the following:
      Cygwin Installation Progress
    13. Once the Cygwin installation process completes, the dialog will just close.  There is no confirmation message.
    14. You should now see the E-TextEditor window with a blank document in front of you. 
    15. At the bottom, click where it says “Plain Text” to open the dialect menu.  Select “Ruby” (not “Ruby on Rails”) from the list.
    16. In the main document window, type the following: puts ‘Hello World’
    17. Press Ctrl+R to run the program.
    18. It is very likely that you will see a big error message in the output window rather than the expected program output.  If that is the case then proceed with the steps below.  Otherwise, congratulations on writing your first Ruby program!
    19. Close E-TextEditor.
    20. Open a Windows command prompt:
      1. Press WindowsKey+R on the keyboard to open the Run dialog.
      2. Type “cmd” in the box and press the Enter key.
    21. Navigate to the Cygwin bin directory, which is most likely located in: C:\cygwin\bin. You’ll need to change into this directory using the cd command.
    22. Type ash.exe to run the shell.
    23. You should see a $ symbol at the start of the line.
    24. Type ./rebaseall and press the Enter key.
    25. Once this finishes and you see another $ on the next line, type exit and press the Enter key.  Then close the command prompt window.
      Command Prompt Output
    26. Open the E-TextEditor program and (assuming your program code is still there) press Ctrl+R to run your program again.
    27. The program should now run correctly and you should see “Hello World” printed in the output section. Congratulations on your first successful Ruby program execution.
      Successful Execution
       

    This concludes the setup process for E-TextEditor and Cygwin.