|Things that might matter|
|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:
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. Rails: assets controller generator helper integration_test mailer migration model observer performance_test resource scaffold scaffold_controller session_migration task Coffee: coffee:assets Jquery: jquery:install Js: js:assets
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:
Usage: rails generate controller NAME [action action] [options] 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 Description: 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. Example: `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:
Alright! Now according to my calculations we should see something at /home/index. Make sure your rails server is fired up…
…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.
Understanding how a programming language works is a surefire way to be a better developer. Read more.