ebook img

Ruby on Rails™ Tutorial PDF

589 Pages·2012·12.95 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Ruby on Rails™ Tutorial

ptg8286261 Praise for Michael Hartl’s Books and Videos on Ruby on RailsTM ptg8286261 ‘‘Myformercompany(CDBaby)wasoneofthefirsttoloudlyswitchtoRubyon Rails,andthenevenmoreloudlyswitchbacktoPHP(Googlemetoreadaboutthe drama).ThisbookbyMichaelHartlcamesohighlyrecommendedthatIhadtotry it,andtheRubyonRailsTMTutorial iswhatIusedtoswitchbacktoRailsagain.’’ —FromtheForewordbyDerekSivers(sivers.org) Formerly:Founder,CDBaby Currently:Founder,ThoughtsLtd. ‘‘Michael Hartl’s Rails Tutorial book is the #1 (and only, in my opinion) place to startwhenitcomestobooksaboutlearningRails....It’sanamazingpieceofwork and, unusually, walks you through building a Rails app from start to finish with testing.IfyouwanttoreadjustonebookandfeellikeaRailsmasterbytheendof it,picktheRubyonRailsTMTutorial.’’ —PeterCooper Editor,RubyInside ‘‘Groundedintherealworld.’’ —IProgrammer(www.i-programmer.info),byIanElliot ‘‘Thebookgivesyouthetheoryandpractice,whilethevideosfocusonshowingyou inpersonhowitsdone.Highlyrecommendedcombo.’’ —AntonioCangiano,SoftwareEngineer,IBM ‘‘TheauthorisclearlyanexpertattheRubylanguageandtheRailsframework,but more than that, he is a working software engineer who introduces best practices throughoutthetext.’’ —GregCharles,SeniorSoftwareDeveloper,FairwayTechnologies ‘‘Overall,thesevideotutorialsshouldbeagreatresourceforanyonenewtoRails.’’ —MichaelMorin,ruby.about.com ‘‘Hands-down,IwouldrecommendthisbooktoanyonewantingtogetintoRuby onRailsdevelopment.’’ —MichaelCrump,MicrosoftMVP ptg8286261 RUBY ONRAILSTM TUTORIAL Second Edition ptg8286261 Visit informit.com/ruby for a complete list of available products. ptg8286261 TIhe Addison-Wesley Professional Ruby Series provides readers with practical, people-oriented, and in-depth information about applying the Ruby platform to create dynamic technology solutions. The series is based on the premise that the need for expert reference books, written by experienced practitioners, will never be satisfied solely by blogs and the Internet. RUBY ONRAILSTM TUTORIAL Learn Web Developments with Rails Second Edition ptg8286261 Michael Hartl UpperSaddleRiver,NJ•Boston•Indianapolis•SanFrancisco NewYork•Toronto•Montreal•London•Munich•Paris•Madrid Capetown•Sydney•Tokyo•Singapore•MexicoCity Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimed Editor-in-Chief astrademarks.Wherethosedesignationsappearinthisbook,andthepublisherwasawareofa MarkTaub trademarkclaim,thedesignationshavebeenprintedwithinitialcapitallettersorinallcapitals. ExecutiveEditor Theauthorandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedor DebraWilliamsCauley impliedwarrantyofanykindandassumenoresponsibilityforerrorsoromissions.Noliabilityis ManagingEditor assumedforincidentalorconsequentialdamagesinconnectionwithorarisingoutoftheuseofthe JohnFuller informationorprogramscontainedherein. Full-ServiceProduction Thepublisheroffersexcellentdiscountsonthisbookwhenorderedinquantityforbulkpurchasesor Manager JulieB.Nahil specialsales,whichmayincludeelectronicversionsand/orcustomcoversandcontentparticularto yourbusiness,traininggoals,marketingfocus,andbrandinginterests.Formoreinformation,please ProjectManager contact: Laserwords ptg8286261 U.S.CorporateandGovernmentSales CopyEditor Laserwords (800)382-3419 [email protected] Indexer Laserwords ForsalesoutsidetheUnitedStatespleasecontact: InternationalSales Proofreader [email protected] Laserwords Reviewer VisitusontheWeb:informit.com/aw JenniferLindner Cataloging-in-PublicationDataisonfilewiththeLibraryofCongress. PublishingCoordinator KimBoedigheimer Copyright©2013MichaelHartl CoverDesigner Allrightsreserved.PrintedintheUnitedStatesofAmerica.Thispublicationisprotectedby ChutiPrasertsith copyright,andpermissionmustbeobtainedfromthepublisherpriortoanyprohibitedreproduction, Compositor storageinaretrievalsystem,ortransmissioninanyformorbyanymeans,electronic,mechanical, Laserwords photocopying,recording,orlikewise.Toobtainpermissiontousematerialfromthiswork,please submitawrittenrequesttoPearsonEducation,Inc.,PermissionsDepartment,OneLakeStreet, UpperSaddleRiver,NewJersey07458,oryoumayfaxyourrequestto(201)236-3290. ThesourcecodeinRubyonRailsTMTutorialisreleasedundertheMITLicense. ISBN13:978-0-321-83205-4 ISBN10: 0-321-83205-1 TextprintedintheUnitedStatesonrecycledpaperatEdwardsBrothersMalloyinAnnArbor, Michigan. Firstprinting,July2012 Contents ForewordtotheFirstEditionbyDerekSivers xv ForewordtotheFirstEditionbyObieFernandez xvii Acknowledgments xix ptg8286261 AbouttheAuthor xxi Chapter1 FromZerotoDeploy 1 1.1 Introduction 3 1.1.1 CommentsforVariousReaders 4 1.1.2 ‘‘Scaling’’Rails 7 1.1.3 ConventionsinThisBook 7 1.2 UpandRunning 9 1.2.1 DevelopmentEnvironments 10 1.2.2 Ruby,RubyGems,Rails,andGit 12 1.2.3 TheFirstApplication 17 1.2.4 Bundler 19 1.2.5 rails server 23 1.2.6 Model-view-controller(MVC) 25 1.3 VersionControlwithGit 27 1.3.1 InstallationandSetup 27 1.3.2 AddingandCommitting 30 1.3.3 WhatGoodDoesGitDoYou? 31 vii viii Contents 1.3.4 GitHub 32 1.3.5 Branch,Edit,Commit,Merge 34 1.4 Deploying 39 1.4.1 HerokuSetup 39 1.4.2 HerokuDeployment,StepOne 40 1.4.3 HerokuDeployment,StepTwo 40 1.4.4 HerokuCommands 41 1.5 Conclusion 43 Chapter2 ADemoApp 45 2.1 PlanningtheApplication 45 2.1.1 ModelingDemoUsers 47 2.1.2 ModelingDemoMicroposts 48 2.2 TheUsersResource 49 2.2.1 AUserTour 51 2.2.2 MVCinAction 56 2.2.3 WeaknessesofthisUsersResource 62 2.3 TheMicropostsResource 63 ptg8286261 2.3.1 AMicropostMicrotour 63 2.3.2 PuttingthemicroinMicroposts 66 2.3.3 AUserhas manyMicroposts 68 2.3.4 InheritanceHierarchies 70 2.3.5 DeployingtheDemoApp 73 2.4 Conclusion 74 Chapter3 MostlyStaticPages 77 3.1 StaticPages 82 3.1.1 TrulyStaticPages 82 3.1.2 StaticPageswithRails 85 3.2 OurFirstTests 93 3.2.1 Test-drivenDevelopment 93 3.2.2 AddingaPage 99 3.3 SlightlyDynamicPages 103 3.3.1 TestingaTitleChange 103 3.3.2 PassingTitleTests 106 3.3.3 EmbeddedRuby 108 3.3.4 EliminatingDuplicationwithLayouts 111 3.4 Conclusion 114 Contents ix 3.5 Exercises 114 3.6 AdvancedSetup 117 3.6.1 Eliminatingbundle exec 118 3.6.2 AutomatedTestswithGuard 120 3.6.3 SpeedingupTestswithSpork 123 3.6.4 TestsinsideSublimeText 127 Chapter4 Rails-FlavoredRuby 129 4.1 Motivation 129 4.2 StringsandMethods 134 4.2.1 Comments 134 4.2.2 Strings 135 4.2.3 ObjectsandMessagePassing 138 4.2.4 MethodDefinitions 141 4.2.5 BacktotheTitleHelper 142 4.3 OtherDataStructures 142 4.3.1 ArraysandRanges 142 4.3.2 Blocks 146 ptg8286261 4.3.3 HashesandSymbols 148 4.3.4 CSSrevisited 152 4.4 RubyClasses 153 4.4.1 Constructors 153 4.4.2 ClassInheritance 155 4.4.3 ModifyingBuilt-inClasses 158 4.4.4 AControllerClass 159 4.4.5 AUserClass 161 4.5 Conclusion 164 4.6 Exercises 164 Chapter5 FillingintheLayout 167 5.1 AddingSomeStructure 167 5.1.1 SiteNavigation 169 5.1.2 BootstrapandCustomCSS 175 5.1.3 Partials 181 5.2 SassandtheAssetPipeline 187 5.2.1 TheAssetPipeline 187 5.2.2 SyntacticallyAwesomeStylesheets 190

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.