I have already introduced something about the ruby language. Next I will start to learn the rails platform that makes Ruby popular.
The rails platform can build standard MVC applications. MVC is the controller, view, and model.
The command for creating a rails project is rails projectname, which can be created by specifying the database type, such as rails-D MySQL projectname.
Create a demo project: rails-D MySQL demo
Then you can use webrick to start the HTTP server. The startup method is as follows:
Go to the created demo directory and run Ruby script \ Server
The default port is port 3000. You can enter localhost: 3000 to view the page.
In this case, we first create a controller and then create an action in the controller.
Ruby script \ generate controller say
After running the SDK, an app \ controllers \ say_contrller.rb is created.
There is only one empty class saycontroller inherited from applicationcontroller.
Then add a method hello
Class Saycontroller < Applicationcontroller
Def Hello
@ Time = Time. Now
End
End
At this time, enter http: // localhost: 3000/say/hello in the browser to view the template is missing.
Because rails does not find the view of the action "hello", we create a file named "Hello. rhtml" in the "views \ say \" directory, as shown below:
< Html >
< Head >
< Title > Hello! </ Title >
</ Head >
< Body >
< H1 > Hello from rails! </ H1 >
< P > It is now <% = @ Time %> </ P >
</ Body >
</ Html >
In this way, the first example of Hello is displayed.