Generate a Controller and View With Rails 3

Things that might matter
Ruby: 1.9.3
Rails: 3.2
OS: MacOS X Snow Leopard

So you’ve got Rails installed, created a new project and ran it using the built-in server. (If you haven’t check out the previous posts in this series). On our Rails welcome page we should see the following:

Let’s do just that. Open the terminal and make sure your in the directory of your Rails project. Type the following:

rails generate

You should get the following information spit back at you:

Usage: rails generate GENERATOR [args] [options]

General options:
  -h, [--help]     # Print generator's options and usage
  -p, [--pretend]  # Run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
  -q, [--quiet]    # Suppress status output

Please choose a generator below.





Nice. Keep in mind I’m writing this tutorial as I explore Rails myself. I have used other MVC frameworks such as ASP.NET and Codeigniter and therefore understand MVC in general. If you don’t you may want to look for a more generalized in-depth tutorial explaining MVC concepts.

I figure a good way to start my project would be to generate a controller. To get some help with this I enter:

rails generate controller -h

And get this:

  rails generate controller NAME [action action] [options]

      [--skip-namespace]        # Skip namespace (affects only isolated applications)
      [--old-style-hash]        # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
  -e, [--template-engine=NAME]  # Template engine to be invoked
                                # Default: erb
  -t, [--test-framework=NAME]   # Test framework to be invoked
                                # Default: test_unit
      [--helper]                # Indicates when to generate helper
                                # Default: true
      [--assets]                # Indicates when to generate assets
                                # Default: true

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist

    Stubs out a new controller and its views. Pass the controller name, either
    CamelCased or under_scored, and a list of views as arguments.

    To create a controller within a module, specify the controller name as a
    path like 'parent_module/controller_name'.

    This generates a controller class in app/controllers and invokes helper,
    template engine and test framework generators.

    `rails generate controller CreditCard open debit credit close`

    Credit card controller with URLs like /credit_card/debit.
        Controller:      app/controllers/credit_card_controller.rb
        Functional Test: test/functional/credit_card_controller_test.rb
        Views:           app/views/credit_card/debit.html.erb [...]
        Helper:          app/helpers/credit_card_helper.rb

By looking at that example I think I should be able to create by “home” controller and default view like this (“index” is the default view for a controller):

rails generate controller Home index

Rails should squeeze out a few brand new files for you. Your project should now look a little something like this:

Screen capture from SublimeText 2.

Alright! Now according to my calculations we should see something at /home/index. Make sure your rails server is fired up…

rails generate

…and then check out http://localhost:3000/home/index. If it all worked out you should see something like this:

The HTML you see here is in the view file found in app/views/home/index.erb.html. You can see the naming of the folder and file reflects the URL. The same pattern is found in the ‘controller’ folder.

So there you go! Sell all your stuff and move into a remote cabin in the woods.

Creating your first programming language is easier than you think.

Understanding how a programming language works is a surefire way to be a better developer. Read more.

Suggested Reading

written by Ryan Bosinger
Ryan is a web developer living and working in Banff Alberta. He also writes for giftsforpeoplewitheverything.com and freefoley.com.

6 Responses to “Generate a Controller and View With Rails 3”

  1. Many thanks a lot intended for expressing the following effortlessly people you undoubtedly recognize what you are speaking about! Saved as a favorite. Remember to on top of that speak with this site Means). We will have a very hyperlink buy and sell deal amongst us

  2. Great article! That is the kind of info that are meant to be shared
    across the internet. Shame on the seek engines for not positioning
    this publish upper! Come on over and consult with my website .
    Thank you =)

  3. mobile games says:

    I was curious if you ever thought of changing the layout of your site?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could
    connect with it better. Youve got an awful lot of text for only having one or 2 images.
    Maybe you could space it out better?

  4. Seriously! This really is a particular of the most useful web sites We’ve got. ever get there all over with this issue. Essentially Terrific. I’m in addition a pro within this subject matter in order to comprehend your. hard work.

  5. sbobet says:

    Hello Dear, are you actually visiting this
    site on a regular basis, if so then you will absolutely obtain good know-how.

  6. sbobet says:

    Simply want to say your article is as astonishing.
    The clarity in your post is simply nice and i could assume you’re an expert on this subject.
    Well with your permission allow me to grab your feed to
    keep updated with forthcoming post. Thanks a million and please carry on the enjoyable work.

Leave a Reply