Friday, November 16, 2007

7 Steps To A Ruby On Rails Application

1.Install Ruby and Rails

  • I'm using Ubuntu and did this with the Synaptic package manager.
  • I'm also going to try InstantRails, http://rubyforge.org/frs/?group_id=904


2. Generate the application's framework.

$ cd <work_root>
$ rails MyApp
$ cd MyApp


3. Create a Controller.
$ script/generate controller MyControl
NOTE: With Windows and Instant Rails, preface the script commands with ruby.

4. Edit the Controller. Add an action, some_action
$ gedit app/controllers/my_control_controller.rb

class MyControlController < ApplicationController
def some_action
@time = Time.now
end
end


5. Add the view associated with the action.
$ gedit app/views/my_control/some_action.rhtml

<html>
<head>
<title>Action!</title>
</head>
<body>
<ul>
<li>It is now <%= @time -%>
</ul>
<body>
</html>

6. Start the server.
$ script/server
NOTE: With Windows and Instant Rails, preface the script commands with ruby.

7. View the result in a browser.
http://localhost:3000/my_control/some_action


Note: (From Agile Web Development with Rails by Dave Thomas and David Heinemeier Hansson)

In Ruby, the convention is to have variable names where the letters are all lowercase and words are separated by underscores. Classes and modules are named differently:
there are no underscores, and each word in the phrase (including the first) is
capitalized.

Rails controllers have additional naming conventions. If our application has a store controller, then the following happens.
  • Rails assumes the class is called StoreController and that it’s in a file named store_controller.rb in the app/controllers directory.
  • •It also assumes there’s a helper module named StoreHelper in the file store_helper.rb located in the app/helpers directory.
  • It will look for view templates for this controller in the app/views/store directory.
  • It will by default take the output of these views and wrap them in the layout template contained in the file store.rhtml or store.rxml in the directory app/views/layouts.

No comments: