world wide web万维网英文课件:ch15-Introduction to Rails

上传人:汽*** 文档编号:587486808 上传时间:2024-09-06 格式:PPT 页数:16 大小:883KB
返回 下载 相关 举报
world wide web万维网英文课件:ch15-Introduction to Rails_第1页
第1页 / 共16页
world wide web万维网英文课件:ch15-Introduction to Rails_第2页
第2页 / 共16页
world wide web万维网英文课件:ch15-Introduction to Rails_第3页
第3页 / 共16页
world wide web万维网英文课件:ch15-Introduction to Rails_第4页
第4页 / 共16页
world wide web万维网英文课件:ch15-Introduction to Rails_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《world wide web万维网英文课件:ch15-Introduction to Rails》由会员分享,可在线阅读,更多相关《world wide web万维网英文课件:ch15-Introduction to Rails(16页珍藏版)》请在金锄头文库上搜索。

1、Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-WesleyChapter 15Introduction to Rails2-2Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.1 Overview of RailsRails is Ruby based“A development framework for Web-based applications”Rails uses the Model-Vi

2、ew-Controller architectureModel classes are the data classes, including constraint enforcementView classes present the data to the userController classes perform computations and deal with user interactionRails uses an Object-Relational Mapping approach to working with databasesA cars table correspo

3、nds to a car classThe rows of the table correspond to instances of the classThe correspondence is built automatically by the Rails frameworkRails uses a combination of Ruby code and template files to create responses2-3Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.1 D

4、eveloper TasksDesign and build the model, including a database for storing model dataDesign and implement actionsDesign and implement the views2-4Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.2 Document requestsThe Rails framework provides much of the superstructure n

5、ecessary for a web applicationScripts with the Rails framework set up the basic structureThe first Rails example serves a single static document2-5Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.3 Project SetupThe InstantRails package provides all the components needed

6、to develop and test Rails projects on a Microsoft Windows platformStart the InstantRails consoleInstantRailsSet up a rails application rails rails1Generate a controllerruby script/generate controller sayRun the default serverruby script/serverThe server runs for a particular application2-6Copyright

7、2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.3 Project Directory Structure2-7Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.2 Project ComponentsA controller class, SayController, in the controllers directoryThe controller class has method named h

8、elloAn html template file (static for this example) named hello.rhtml in directory viewssayThe Rails framework associates these components by the name say2-8Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.2 Request Processing2-9Copyright 2008 Pearson Education, Inc. Pub

9、lishing as Pearson Addison-Wesley15.2 Dynamic DocumentsThe next example displays the greeting but also displays the current timeUses Time.now from the Ruby library embeds Ruby code in template files causes the value of the executed code to be inserted in placeInstance variables of the controller cla

10、ss can be accessed in the template file2-10Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.3 Processing FormsThe popcorn example is used to illustrate accessing information from forms2-11Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.3 Set

11、ting Up the ApplicationsA controller home is createdAn empty action method the_form is createdA static template file the_form.rhtml is created to display the initial data entry formThe action on the form is simply “result” which links to an action method named result in the same controller class2-12

12、Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.3 The Controller and the ViewAction method result in the controller classGet form dataCompute resultsObject params holds the form data in a hash-like objectCan be indexed by symbols or by strings using widget namesparams:p

13、hone gets the phone numberparams:unpop.to_i gets the unpopped amount as an integerThe sprintf method can be used to format dataUsed with format specification %5.2d to format floating numbers with exactly two decimal places2-13Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesle

14、y15.4 Rails Applications with DatabasesThis example uses the Corvettes databaseThe user specifies model year limits and body stylesThe response is a list of matching cars2-14Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.4 Building the DatabaseA database is created wit

15、h four tables using MySQL commandsA script file is used to make the typing easierTables are named with plural names because of Rails names corresponding classes with the singularModel classes are createdruby script/generate model corvetteDirectives in the model files specify the table relationshas_m

16、anybelongs_to2-15Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.4 Building the ApplicationA table object gives access to the data in the tableMethod count gives the number of rowsMethod find is used to search Method findOne parameter is a primary key search. The matchi

17、ng row is returnedParameter :all requires a second parameter giving the value of the :conditions symbol, a test condition. All matching rows are returnedParameter :first requires a second parameter giving the value of the :conditions symbol, a test condition. The first matching row is returnedMethod

18、 find_by_sql takes an SQL SELECT commandThe action methods query the database and put results into instance variablesThe matching template displays the results2-16Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley15.5 LayoutsA layout template provides a standard template for other templatesPut in the layouts directoryPut a layout command in the ApplicationController classThe layout template can provide header and styling directions while other templates provide contentThe content_for_layout variable in the layout template provides the content from the other template

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号