Ruby journey (8) Hello world of rails!

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.