Small exploration of rails program running sequence

Source: Internet
Author: User
Document directory
  • 1. Create a project
  • 2. Add a controller
  • 3. Delete the newly added Controller
  • Template is missing
  • Unknown action
  • Template is missing

Some beginners asked me, what is the sequence of running a new rails program and adding a controller? Instance description.

Environment:

Ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]

Rails 3.2.8

1. Create a project

Run rails new mytest1

2. Add a controller

Run the rails generate controller page, which is shown as follows:

      create  app/controllers/page_controller.rb      invoke  erb      create    app/views/page      invoke  test_unit      create    test/functional/page_controller_test.rb      invoke  helper      create    app/helpers/page_helper.rb      invoke    test_unit      create      test/unit/helpers/page_helper_test.rb      invoke  assets      invoke    coffee      create      app/assets/javascripts/page.js.coffee      invoke    scss      create      app/assets/stylesheets/page.css.scss

If an input error is found, you can cancel the creation of the controller. For example, if a letter is entered incorrectly, it is written as rails generate controller pgae.

3. Delete the newly added Controller

Run rails destroy controller pgae, which will be displayed as follows. Note that even if the pgae controller is not available, rails will not check whether the pgae controller exists in the project.

Remove APP/controllers/pgae_controller.rb
Invoke ERB
Remove APP/views/pgae
Invoke test_unit
Remove test/functional/pgae_controller_test.rb
Invoke helper
Remove APP/helpers/pgae_helper.rb
Invoke test_unit
Remove test/unit/helpers/pgae_helper_test.rb
Invoke assets
Invoke coffee
Remove APP/assets/javascripts/pgae. js. Coffee
Invoke SCSs
Remove APP/assets/stylesheets/pgae.css. SCSs

4. Start the Web Server

Run rails server-P 4000. Because a program occupies port 3000, the-P (port) parameter is used to specify port 4000 when starting this program.

If it is normal, It is shown as follows. If it cannot be started, check whether port 4000 is occupied.

=> Booting webrick
=> Rails 3.2.8 application starting in development on http: // 0.0.0.0: 4000
=> Call with-D to detach
=> Ctrl-C to shutdown Server
[00:31:36] info webrick 1.3.1
[00:31:36] info Ruby 1.9.3 () [i686-linux]
[00:31:36] info webrick: httpserver # Start: pid = 3096 Port = 4000

5. Test pagecontroller

 

(The IP address of the server where rails is located is 192.168.1.107). in Linux, use the ifconfig command to view the IP address, which is only one letter different from Windows ipconfig. See eth0 as the local Nic)

Eth0 link encap: Ethernet hwaddr 00: 0C: 29: 19: F2: 8C
Inet ADDR: 192.168.1.107 bcast: 255.255.255.255 mask: 255.255.255.0
Up broadcast running Multicast MTU: 576 Metric: 1
RX packets: 71263 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 64421 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX Bytes: 10857067 (10.8 MB) TX Bytes: 11945904 (11.9 MB)
Interrupt: 19 base address: 0x2000

Lo link encap: local loopback
Inet ADDR: 127.0.0.1 mask: 255.0.0.0
Inet6 ADDR: 1/128 scope: Host
Up loopback running MTU: 16436 Metric: 1
RX packets: 2289 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 2289 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 0
RX Bytes: 230245 (230.2 KB) TX Bytes: 230245 (230.2 KB)

Open the browser and enter http: // 192.168.1.107: 4000/page. An error occurs when no route is displayed.

Routing Error
No route matches [GET] "/pages"

Try runningrake routesFor more information on available routes.

6. Add a default route

Add resources: page in config/routes. RB of the project.

Mytest1: application. routes. Draw do
Resources: Page
End

After the page is refreshed, there is still no route error. Resources: page actually generates the rails agreed route.

7. view all current routes

Run rake routes. When you directly enter http: // 192.168.1.107: 4000/pages, the pages/index actually executed will not have the action named index in our controller.

Result:

page_index GET    /page(.:format)          page#index           POST   /page(.:format)          page#create  new_page GET    /page/new(.:format)      page#new edit_page GET    /page/:id/edit(.:format) page#edit      page GET    /page/:id(.:format)      page#show           PUT    /page/:id(.:format)      page#update           DELETE /page/:id(.:format)      page#destroy
8. Add an action named Index

Add the simplest index to APP/controllers/page_controller.rb.

class PageController < ApplicationController    def index    endend
9. template loss

Refresh http: // 192.168.1.107: 4000/pages and get the following prompt:

Template is missing

Missing template page/index, application/index with {: locale => [: En],: formats => [: HTML],: handlers => [: Erb,: builder,: coffee]}. searched in: * "/home/railsu/project/mytest1/APP/views"

The error message has already told you that there is no page/index template. Therefore, we can view index.html under APP/views/page. erb, the name is agreed, the view template and the action name in the controller must be consistent, the suffix is HTML. erb is the end, as follows:

Refresh the page to view the input content in the view.

Through this very simple process, we can see that when requesting page from a browser, we will find the corresponding pagecontroller and then execute action (index ), find view/Page (same as controller name)/index.html. the View File ERB to display.

 

Next, add an action named home.

Class pagecontroller <applicationcontroller
Def Index
End
Def home

End
End

10. Unknown action

Enter http: // 192.168.1.107: 4000/page/home in the browser to access this action. An error occurs. This action is clearly displayed. How can this problem be unknown?

Error message:

Unknown action

The action 'show 'cocould not be found for pagecontroller

Check whether this action exists in the rails console.

11. Open the rails Console

Railsu @ angestudy :~ /Project/mytest1 $ rails Console
Loading Development Environment (rails 3.2.8)
12. view the action in the controller.

In the project (enter mytest1), execute the rails console, enter the following command, and observe the result. The Home Action exists.

 

P = pagecontroller. New
==## <Pagecontroller: 0xa602a70 @ _ routes = nil, @ _ action_has_layout = true, @ _ headers = {"Content-Type" => "text/html "}, status = 200, _ Request = nil, _ response = nil>
P. Methods. grep (/home /)
=> [: Home]

13. Custom Routing

Add get 'page/home' before resources: page. Custom routes are generally written before automatically generated routes. Otherwise, they will be overwritten by automatically generated routing rules.

Mytest1: application. routes. Draw do
Get 'page/home'
Resources: Page
End

 

We are very happy to see the loss of the template. It indicates that the home action has been found. The rest can be freely tossed.

Template is missing

Missing template page/home, application/home with {: locale => [: En],: formats => [: HTML],: handlers => [: Erb,: builder,: coffee]}. searched in: * "/home/railsu/project/mytest1/APP/views"

 

14. Simple text rendering

Add a piece of code in home:

Class pagecontroller <applicationcontroller
Def Index
End
Def home
Render: text => "this is home"

End
End

When you access http: // 192.168.1.107: 4000/page/home, you will find that the template loss error disappears and becomes like this:

This is actually the entire process from controller to view. If you do not write this sentence, rails will automatically find the corresponding view and then execute render.

 

Want to be more complicated? Define a variable and put it in text as follows:

Class pagecontroller <applicationcontroller
Def Index
End
Def home
# Simple rendering of a piece of text is rarely used. After learning to do so, you will basically use views for data presentation.
@ Title = "Return Home"
Render: text => "this is home # {@ title }"
End
End
Refresh the page and process the string before rendering, and then send the result to the browser:

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.