Rails Learning Notes First (building a toy project)

Source: Internet
Author: User
Tags add time virtual environment ruby on rails

Rails learning is based on the Ruby on Rails tutorial, Agile Web Development with rails 4, API documentation for the official website and a guide from Ruby-china (HTTP/ guides.ruby-china.org/index.html) to learn, as described in the directory sequence of Agile Web development with Rails 4 as a whole.


Good learning starts with creating a project.

Rails comes with a complete set of command-line tools

First create the work directory and enter

Then use the Rails new demo

A project named demo was created

As shown

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/8C/wKioL1SdVaqDM1ZjAAHTUc9CcAo856.jpg "title=" ( 54DHJWEGDE{EHS{F40GS) h.jpg "alt=" Wkiol1sdvaqdm1zjaahtuc9ccao856.jpg "/>

When the description is completed as shown

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/8F/wKiom1SdVbnx-LPoAABIM6AKdxs999.jpg "title=" k5qsnt %l9uitik$pd2iz@p9.jpg "alt=" Wkiom1sdvbnx-lpoaabim6akdxs999.jpg "/>

Project structure as shown

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1SdVrbxAvQQAADQgGPVXfk879.jpg "title=" TJ@KJEIEWSV) (b~pks1ehm7.jpg "alt=" Wkiom1sdvrbxavqqaadqggpvxfk879.jpg "/>

Is the purpose of the various folders: Graph Source: http://guides.ruby-china.org/index.html

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8C/wKioL1SdX1ay1lZeAAOwe-TI5V8373.jpg "title=" Jvacra _oxvhdwc87um~ (gfx.jpg "alt=" Wkiol1sdx1ay1lzeaaowe-ti5v8373.jpg "/>

Perform rake baout to view installation information (only part of the content is intercepted)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8C/wKioL1SdWLuh-hz8AAHA39Fu1Pw272.jpg "title=" yy~sx_ [~VQH] Ah_}[_}s8ga.jpg "alt=" Wkiol1sdwluh-hz8aaha39fu1pw272.jpg "/>

Rake about also detects if there is an installation error

After everything OK, execute rails s (shorthand for Rails server)

Since this virtual environment already has Nodejs installed, there is no JavaScript runtime error to perform properly

The results are as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/8C/wKioL1SdWa6At5iHAAFcIxST9VI077.jpg "title=" 2 ' C ' o@ }[(${CC ((3{(6) 7kh.png "alt=" Wkiol1sdwa6at5ihaafcixst9vi077.jpg "/>

Vagrant has set the private_network and specified IP as 192.168.33.10, so the direct access http://192.168.33.10:3000/results are as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1SdWjDCSZNZAAMLIW3qm04256.jpg "title=" i709[ Ru1oa ' k1o$ctvo%~6a.jpg "alt=" wkiom1sdwjdcsznzaamliw3qm04256.jpg "/> See also hello,rails ... Why should I add a 冏?

Rails g Controller Say Hello Goodbye (rails generate) for building controllers

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1SdXxTQvBzmAAGlZkhEwtU568.jpg "title=" @_ 4x0n1t$j ' $DQ 84{%k_z@d.jpg "alt=" Wkiom1sdxxtqvbzmaaglzkhewtu568.jpg "/>

Open DEMO/APP/CONTROLLERS/SAY_CONTROLLER.RB build results as follows

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1SdYDbCdQAHAACK3BDqSQ0564.jpg "title=" ~i~dw% 1oagdp@f9aq[5fv5r.jpg "alt=" Wkiom1sdydbcdqahaack3bdqsq0564.jpg "/>

Rails vs. Request URL

Access Http://192.168.33.10:3000/say/hello after starting the server

Appear:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8F/wKiom1SdYRyjjr2eAACT6PzLT5U074.jpg "title="% 8c1mos[bfqgdy@ ' @}f0uh0.jpg "alt=" Wkiom1sdyryjjr2eaact6pzlt5u074.jpg "/>

, Rails looks for template files with the same name as the controller by default, and opens the template file as shown below

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8F/wKiom1SdY9WQtuIgAABplvj2N7w908.jpg "title=" y]@ 71W0JYC} @N27}~q[e}$2.jpg "alt=" Wkiom1sdy9wqtuigaabplvj2n7w908.jpg "/>

After modification:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8D/wKioL1SdZO3yAMJPAABbKy12qzo067.jpg "title=" 4p__} ( 8YB ' 68$ $S [5[] ol5s.jpg "alt=" Wkiol1sdzo3yamjpaabbky12qzo067.jpg "/>

After refreshing the viewer

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1SdZOKQ1yNBAACBDtsLJQE218.jpg "title=" f@1u~ 6k@0qwx ' S7 ' {68% $T 1.jpg "alt=" Wkiom1sdzokq1ynbaacbdtsljqe218.jpg "/>

The execution process for a request is as follows: (from the Ruby on Rails tutorial)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/8F/wKiom1SdZ1OAbd5zAADWZ0-eul8345.jpg "title=" xp20v) Re6[fpe7ys16xtvye.png "alt=" Wkiom1sdz1oabd5zaadwz0-eul8345.jpg "/>

Rails accepts a request, handles it by the controller, decides what to do next, renders the view immediately, returns it to the viewer, or interacts with the model to render the view first. Writing here, a little curious about Django's MTV model, have a good time to ponder.

Add Point dynamic Content

To open the template file, modify the following:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/8D/wKioL1SdadrDGvqhAAB8Uz_N6nw583.jpg "title=" 1JTEOM }s ' 0~s7) 2gv{z@yma.jpg "alt=" Wkiol1sdadrdgvqhaab8uz_n6nw583.jpg "/>


Refresh the page to show the following:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/8F/wKiom1SdaW-i-qouAABlY3aPQKE404.jpg "title=" Qovjqsodbsiyyn2u_cqkurw.jpg "alt=" Wkiom1sdaw-i-qouaably3apqke404.jpg "/>

Add time to the improved version of the wording

Controller:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8D/wKioL1SdayeRA4J9AACUo-tg7lo200.jpg "title=" yzf~[p %{6ORCV (tshh@fc[b.jpg "alt=" Wkiol1sdayera4j9aacuo-tg7lo200.jpg "/>

Template:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8F/wKiom1Sdap7Q1Bd8AABwhOhY7eE418.jpg "title=" 8QPM "@ @71g4u6} ' @x% $H _im.jpg "alt=" Wkiom1sdap7q1bd8aabwhohy7ee418.jpg "/>

The above code shows the principle that the rails convention is larger than the configuration.


Add link

First modify the Demo/app/views/say/goodbye.html.erb results as follows

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1SdbJvi3ecpAAB7dQ6XQRY051.jpg "title="]7dx3e [s19%$ ' N0mmq@[e7w.jpg "alt=" Wkiom1sdbjvi3ecpaab7dq6xqry051.jpg "/>

The page appears as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8D/wKioL1SdbYLyz1wzAACPr4gcy-c547.jpg "title=" 33z[ imx0s (0a84y[_2~_5v0.jpg "alt=" Wkiol1sdbylyz1wzaacpr4gcy-c547.jpg "/>

Add a link!

Modify the Hello and Goodbye templates as follows:

Hello

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/8D/wKioL1Sdb1_CGk1uAAC9fMScDzU359.jpg "title=" 51 ' Ulw4hiy3atho}ttu ' k]k.jpg "alt=" Wkiol1sdb1_cgk1uaac9fmscdzu359.jpg "/>


Goodbye

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/8F/wKiom1Sdbt2zcUHeAACgVMT1tdE525.jpg "title=" i$m$ 2l25m5eo[{wh1q2}ff7.jpg "alt=" Wkiom1sdbt2zcuheaacgvmt1tde525.jpg "/>


Results:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/8F/wKiom1SdbzGCYS-YAAC44uTyb-c274.jpg "title=" y~ X7d4so7dk82e@~974txfb.jpg "alt=" Wkiom1sdbzgcys-yaac44utyb-c274.jpg "/>

After clicking:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/8F/wKiom1Sdb1mQGmcJAACXxBCENqU880.jpg "title=" At_ Ncyln[]aoe]0[}]myqlc.jpg "alt=" Wkiom1sdb1mqgmcjaacxxbcenqu880.jpg "/>

The second chapter of the book ends here! Sprinkle the flowers!!

Add:

Methods for generating API documentation locally

Rails New ProjectName

CD ProjectName

Rake Doc:rails # Generate API documentation



This article is from the "7692323" blog, please be sure to keep this source http://7702323.blog.51cto.com/7692323/1596391

Rails Learning Notes First (building a toy project)

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.