Create a Controller

As mentioned earlier, a Controller is where much of your Ruby code will live. It is the part of the application that sits between the View (what appears in the browser) and the Model (what happens to the data). Because this is a “Hello world” application, let’s create a Controller to say “hello.” In the spirit of originality, I’ll call this the SayHello controller. Once again, you can create this by running a script at the system prompt. You will need to open another command window in the directory from which you previously ran the server script (for example, C: ailsappshelloworld). You can’t reuse your existing command window because the server is running in that one, and you would need to close it down to get back to the prompt—and that would stop your Rails application from working!

At the prompt, enter this (be sure to use the capitalization of SayHello as shown):

Rails 3

rails generate controller SayHello

Rails 2

ruby script/generate controller SayHello

After a few moments, you will be informed that various files and directories have been created, including the following:

app/views/say_hello
app/controllers/say_hello_controller.rb
test/functional/say_hello_controller_test.rb
app/helpers/say_hello_helper.rb

Note

The generate controller script also creates the file application_controller.rb in Rails 3 or application.rb in Rails 2, which is the controller for the entire application, plus a folder, /views/say_hello, which you will make use of shortly.

Notice how Rails has parsed the name SayHello into two lowercase words, say and hello, separated by an underscore, and it has used this name as the first part of the generated Ruby files such as say_hello_controller.rb. This is just one example of the “configuration by convention” approach that Rails uses.

Locate the controller file say_hello_controller.rb, which has been created in helloworldappcontrollers. Open this file in a text editor. This empty method has been autogenerated:

class SayHelloController < ApplicationController
end

Inside this class you can write some code to be executed when a certain page is displayed. Edit the class definition to match the following:

class SayHelloController < ApplicationController
    def index
        render :text => "Hello world"
    end

    def bye
        render :text => "Bye bye"
    end
end

This now contains two methods, index and bye. Each method contains a single line of code. In spite of the fact that I have omitted parentheses (a parentheses-light style of coding is favored by many Rails developers), you can probably deduce that render is a method that takes a hash as an argument; the hash itself contains a key-value pair comprising a symbol and a string. For parentheses-lovers, the index method can be rewritten like this:

def index
   render( { :text => "Hello world" } )
end

And there you have your first real Rails application. To try it, you need to go back to the web browser and enter the full “address” of the two functions you just wrote. But first you may need to restart your server. Just press ctrl-C in the command window where the server is running. When the server exits, restart by entering the following:

Rails 3

rails server

Rails 2

ruby script/server

There’s just one more thing you have to do in Rails 3. You need to tell it how to find a “route” specified by an address entered into web browser. This step is not required for Rails 2. In Rails 3, open the routes.rb file in your helloworldconfig folder. Now edit it to match the following (or simply uncomment the line of code that you’ll find at the bottom of the file):

match ':controller(/:action(/:id(.:format)))'

You are now ready to test the application. To do so, you just need to enter an address to access a controller method. The address takes the form of the host and port (the same as you entered previously—for example, http://localhost:3000), plus the name of the controller (/say_hello) and finally the name of a specific method (/index or /bye). Try entering these, as shown next, into your browser’s address field, once again ensuring that you are using the appropriate port number if it is not 3000:

http://localhost:3000/say_hello/index
http://localhost:3000/say_hello/bye

Your browser should display “Hello world” and “Bye bye” respectively for each address. If all is working at this point, you can bathe in the warm glow of having created your first Ruby on Rails application. If however, you are seeing MySQL database errors, read Can’t Find the Database? in Configuring MySQL and fix the problem before continuing.

Incidentally, Rails uses the index method as a default so you can use the index view as your home page and omit that part of the URL when entering the address into the browser:

http://localhost:3000/say_hello
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.227.82