ebook img

Ruby on Rails 3 Tutorial PDF

571 Pages·2010·8.98 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 3 Tutorial

www.it-ebooks.info Praise for Ruby on Rails™ 3 Tutorial RailsTutorial.org: Michael Hartl’sawesome new Rails Tutorial TheRubyonRails™3Tutorial:LearnRailsbyExamplebyMichaelHartlhasbecomea mustreadfordeveloperslearninghowtobuildRailsapps. —PeterCooper,editorofRubyInside Very detailed and hands-on Rails Tutorial! Greatjob!I’mlearningRails,andfoundyourtutorialtobeoneofthemostdetailedand hands-on guides. Besides many details of Rails, it also taught me about Git, Heroku, RSpec, Webrat, and most important (at least to me), it emphasized the Test-Driven Development(TDD)methodology.Ilearnedalotfromyourtutorial. Keepupthegoodjob!Thankssomuchforsharingit. —AlbertLiu,seniormanager,AchievoCorporation. Ruby on Rails Tutorial is the best! JustwantedtosaythatyourRubyonRailstutorialisthebest! I’vebeentryingforawhiletowrapmyheadaroundRails.Goingthroughyourtutorial, I’m finally feeling comfortable in the Rails environment. Your pedagogical style of www.it-ebooks.info graduallyintroducingmorecomplextopicswhileatthesametimegivingthereaderthe instantgratificationandasenseofaccomplishmentwithworkingexamplesreallyworks for me. I also like the tips and suggestions that give me a sense of learning from a real Railsinsider.Youre-mailresponsetoaproblemIranintoisanexampleofyourgenerous sharingofyourexperience. —RonBingham,CEO,SounDBuytz I love the writing style of the Rails Tutorial IlovethewritingstyleoftheRailsTutorial,andthereissomuchcontentthatisdifferent fromotherRailsbooksoutthere,makingitthatmuchmorevaluable...Thanksforyour work! —AllenDing www.it-ebooks.info RUBY ONRAILS™ 3TUTORIAL www.it-ebooks.info Addison-Wesley f i l i Pro ess ona Ruby Ser es Obie Fernandez, Series Editor Visit informit.com/ruby for a complete list of available products. The 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. www.it-ebooks.info RUBY ONRAILS™ 3TUTORIAL Learn Rails™ by Example Michael Hartl UpperSaddleRiver,NJ•Boston•Indianapolis•SanFrancisco NewYork•Toronto•Montreal•London•Munich•Paris•Madrid Capetown•Sydney•Tokyo•Singapore•MexicoCity www.it-ebooks.info Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimed Editor-in-Chief MarkTaub astrademarks.Wherethosedesignationsappearinthisbook,andthepublisherwasawareofa trademarkclaim,thedesignationshavebeenprintedwithinitialcapitallettersorinallcapitals. ExecutiveAcquisitionsEditor DebraWilliamsCauley Theauthorandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedor impliedwarrantyofanykindandassumenoresponsibilityforerrorsoromissions.Noliabilityis ManagingEditor assumedforincidentalorconsequentialdamagesinconnectionwithorarisingoutoftheuseofthe JohnFuller informationorprogramscontainedherein. ProjectEditor ElizabethRyan Thepublisheroffersexcellentdiscountsonthisbookwhenorderedinquantityforbulkpurchasesor specialsales,whichmayincludeelectronicversionsand/orcustomcoversandcontentparticularto CopyEditor yourbusiness,traininggoals,marketingfocus,andbrandinginterests.Formoreinformation,please EricaOrloff contact: Indexer ClaireSplan U.S.CorporateandGovernmentSales (800)382-3419 Proofreader [email protected] ClaireSplan ForsalesoutsidetheUnitedStatespleasecontact: PublishingCoordinator KimBoedigheimer InternationalSales [email protected] CoverDesigner GaryAdair VisitusontheWeb:informit.com/aw Compositor LibraryofCongressCataloging-in-PublicationData GlyphInternational Hartl,Michael. Rubyonrails3tutorial:learnRailsbyexample/MichaelHartl. p. cm. Includesindex. ISBN-10:0-321-74312-1(pbk.:alk.paper) ISBN-13:978-0-321-74312-1(pbk.:alk.paper) 1.Rubyonrails(Electronicresource)2.Websitedevelopment.3.Ruby (Computerprogramlanguage)I.Title. TK5105.8885.R83H372011 (cid:2) 005.117–dc22 2010039450 Copyright©2011MichaelHartl Allrightsreserved.PrintedintheUnitedStatesofAmerica.Thispublicationisprotectedby copyright,andpermissionmustbeobtainedfromthepublisherpriortoanyprohibitedreproduction, storageinaretrievalsystem,ortransmissioninanyformorbyanymeans,electronic,mechanical, photocopying,recording,orlikewise.Forinformationregardingpermissions,writeto: PearsonEducation,Inc. RightsandContractsDepartment 501BoylstonStreet,Suite900 Boston,MA02116 Fax:(617)671-3447 ThesourcecodeinRubyonRails™3TutorialisreleasedundertheMITLicense. ISBN13:978-0-321-74312-1 ISBN10: 0-321-74312-1 TextprintedintheUnitedStatesonrecycledpaperatEdwardsBrothersinAnnArbor,Michigan Firstprinting,December2010 www.it-ebooks.info Contents ForewordbyDerekSivers xv ForewordbyObieFernandez xvii Acknowledgments xix 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 9 1.2.2 Ruby,RubyGems,Rails,andGit 11 1.2.3 TheFirstApplication 15 1.2.4 Bundler 16 1.2.5 rails server 20 1.2.6 Model-View-Controller(MVC) 22 1.3 VersionControlwithGit 24 1.3.1 InstallationandSetup 24 1.3.2 AddingandCommitting 26 1.3.3 WhatGoodDoesGitDoYou? 28 1.3.4 GitHub 29 1.3.5 Branch,Edit,Commit,Merge 31 vii www.it-ebooks.info viii Contents 1.4 Deploying 35 1.4.1 HerokuSetup 36 1.4.2 HerokuDeployment,StepOne 37 1.4.3 HerokuDeployment,StepTwo 37 1.4.4 HerokuCommands 39 1.5 Conclusion 40 Chapter2 ADemoApp 41 2.1 PlanningtheApplication 41 2.1.1 ModelingUsers 43 2.1.2 ModelingMicroposts 44 2.2 TheUsersResource 44 2.2.1 AUserTour 46 2.2.2 MVCinAction 49 2.2.3 WeaknessesofThisUsersResource 58 2.3 TheMicropostsResource 58 2.3.1 AMicropostMicrotour 58 2.3.2 PuttingthemicroinMicroposts 61 2.3.3 AUserhas_manyMicroposts 63 2.3.4 InheritanceHierarchies 66 2.3.5 DeployingtheDemoApp 68 2.4 Conclusion 69 Chapter3 MostlyStaticPages 71 3.1 StaticPages 74 3.1.1 TrulyStaticPages 75 3.1.2 StaticPageswithRails 78 3.2 OurFirstTests 84 3.2.1 TestingTools 84 3.2.2 TDD:Red,Green,Refactor 86 3.3 SlightlyDynamicPages 103 3.3.1 TestingaTitleChange 103 3.3.2 PassingTitleTests 106 3.3.3 InstanceVariablesandEmbeddedRuby 108 3.3.4 EliminatingDuplicationwithLayouts 112 3.4 Conclusion 115 3.5 Exercises 116 www.it-ebooks.info Contents ix Chapter4 Rails-FlavoredRuby 119 4.1 Motivation 119 4.1.1 AtitleHelper 119 4.1.2 CascadingStyleSheets 122 4.2 StringsandMethods 125 4.2.1 Comments 125 4.2.2 Strings 126 4.2.3 ObjectsandMessagePassing 129 4.2.4 MethodDefinitions 132 4.2.5 BacktothetitleHelper 133 4.3 OtherDataStructures 134 4.3.1 ArraysandRanges 134 4.3.2 Blocks 137 4.3.3 HashesandSymbols 139 4.3.4 CSSRevisited 142 4.4 RubyClasses 144 4.4.1 Constructors 144 4.4.2 ClassInheritance 145 4.4.3 ModifyingBuilt-InClasses 148 4.4.4 AControllerClass 150 4.4.5 AUserClass 152 4.5 Exercises 154 Chapter5 FillingintheLayout 157 5.1 AddingSomeStructure 157 5.1.1 SiteNavigation 159 5.1.2 CustomCSS 164 5.1.3 Partials 171 5.2 LayoutLinks 177 5.2.1 IntegrationTests 178 5.2.2 RailsRoutes 181 5.2.3 NamedRoutes 183 5.3 UserSignup:AFirstStep 186 5.3.1 UsersController 186 5.3.2 SignupURL 188 5.4 Conclusion 191 5.5 Exercises 191 www.it-ebooks.info

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.