ebook img

Learn Rails 5.2: Accelerated Web Development with Ruby on Rails PDF

444 Pages·2018·5.92 MB·English
by  
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 Learn Rails 5.2: Accelerated Web Development with Ruby on Rails

Learn Rails 5.2 Accelerated Web Development with Ruby on Rails — Stefan Wintermeyer Learn Rails 5.2 Accelerated Web Development with Ruby on Rails Stefan Wintermeyer Learn Rails 5.2: Accelerated Web Development with Ruby on Rails Stefan Wintermeyer Bochum, Germany ISBN-13 (pbk): 978-1-4842-3488-4 ISBN-13 (electronic): 978-1-4842-3489-1 https://doi.org/10.1007/978-1-4842-3489-1 Library of Congress Control Number: 2018939414 Copyright © 2018 by Stefan Wintermeyer This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please email [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484234884. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper Für Oma und Opa. I dedicate this book to my grandparents. Table of Contents About the Author ���������������������������������������������������������������������������������������������������xvii About the Technical Reviewer ��������������������������������������������������������������������������������xix Preface �������������������������������������������������������������������������������������������������������������������xxi Introduction ����������������������������������������������������������������������������������������������������������xxiii Chapter 1: Ruby Introduction ������������������������������������������������������������������������������������1 Ruby 2.5 ........................................................................................................................................1 Basics ...........................................................................................................................................2 Hello World ..............................................................................................................................2 puts and print ..........................................................................................................................2 Comments ...............................................................................................................................3 Help via ri ................................................................................................................................4 irb ............................................................................................................................................4 Ruby Is Object-Oriented ................................................................................................................5 Methods ...................................................................................................................................6 Classes ....................................................................................................................................9 Basic Classes ..............................................................................................................................20 Strings ...................................................................................................................................20 Numbers ................................................................................................................................23 Boolean Values and nil ..........................................................................................................24 Variables .....................................................................................................................................24 Naming Conventions..............................................................................................................25 Scope of Variables .................................................................................................................26 Methods Once Again ...................................................................................................................28 Method Chaining ...................................................................................................................29 Getters and Setters ................................................................................................................29 v Table of ConTenTs Converting from One to the Other: Casting ............................................................................33 Method to_s for Your Own Classes ........................................................................................34 Is + a Method? ......................................................................................................................35 if Condition ..................................................................................................................................37 Shorthand ..............................................................................................................................38 else ........................................................................................................................................39 elsif ........................................................................................................................................39 Loops ..........................................................................................................................................39 while and until .......................................................................................................................39 Blocks and Iterators ..............................................................................................................41 Arrays and Hashes ......................................................................................................................44 Arrays ....................................................................................................................................44 Hashes ...................................................................................................................................46 Range ..........................................................................................................................................48 Chapter 2: First Steps with Rails ����������������������������������������������������������������������������51 Environment (Development) ........................................................................................................51 SQLite3 Database ........................................................................................................................52 Why Is It All in English? ...............................................................................................................52 Static Content (HTML and Graphics Files) ...................................................................................52 Create a Rails Project ............................................................................................................52 Static Pages ...........................................................................................................................55 Creating HTML Dynamically with erb ..........................................................................................57 Programming in an erb File ...................................................................................................60 Layouts ..................................................................................................................................64 Passing Instance Variables from a Controller to a View ........................................................66 Partials ..................................................................................................................................67 The Rails Console ........................................................................................................................71 app.........................................................................................................................................73 What Is a Generator? ..................................................................................................................73 Helper..........................................................................................................................................75 vi Table of ConTenTs Debugging ...................................................................................................................................76 debug.....................................................................................................................................76 Web Console ..........................................................................................................................76 Other Debugging Tools...........................................................................................................79 Rails Lingo ..................................................................................................................................79 Don’t Repeat Yourself ............................................................................................................79 Refactoring ............................................................................................................................79 Convention Over Configuration ..............................................................................................79 Model View Controller Architecture ............................................................................................80 Model .....................................................................................................................................80 View .......................................................................................................................................80 Controller ...............................................................................................................................80 Abbreviations ..............................................................................................................................81 Chapter 3: ActiveRecord �����������������������������������������������������������������������������������������83 Creating a Database/Model .........................................................................................................83 The Attributes id, created_at, and updated_at ......................................................................86 Getters and Setters ................................................................................................................87 Possible Data Types in ActiveRecord .....................................................................................87 Decimal .................................................................................................................................88 Naming Conventions (Country vs. country vs. countries) ......................................................89 Database Configuration .........................................................................................................89 Adding Records ...........................................................................................................................90 create ....................................................................................................................................90 new ........................................................................................................................................92 new_record? .........................................................................................................................93 first, last, and all .........................................................................................................................94 Populating the Database with seeds.rb ......................................................................................97 It’s All Just Ruby Code ...........................................................................................................98 Generating seeds.rb from Existing Data ................................................................................99 vii Table of ConTenTs Searching and Finding with Queries .........................................................................................100 find ......................................................................................................................................101 where ..................................................................................................................................102 order and reverse_order ......................................................................................................108 limit .....................................................................................................................................109 group ...................................................................................................................................110 pluck ....................................................................................................................................110 select ...................................................................................................................................111 first_or_create and first_or_initialize ..................................................................................112 Calculations ..............................................................................................................................112 average ................................................................................................................................112 count....................................................................................................................................113 maximum .............................................................................................................................113 minimum .............................................................................................................................114 sum ......................................................................................................................................114 SQL EXPLAIN .............................................................................................................................114 Batches .....................................................................................................................................115 Editing a Record ........................................................................................................................115 Simple Editing .....................................................................................................................115 Active Model Dirty ...............................................................................................................116 update .................................................................................................................................118 Locking ................................................................................................................................118 has_many, a 1:n Association .....................................................................................................121 Creating Records .................................................................................................................123 Accessing Records ..............................................................................................................126 Searching for Records .........................................................................................................128 delete and destroy ...............................................................................................................130 Options ................................................................................................................................131 Many-to-Many, an n:n Association ............................................................................................133 Preparation ..........................................................................................................................133 The Association ...................................................................................................................134 The Association Works Transparently ..................................................................................135 viii Table of ConTenTs Polymorphic Associations .........................................................................................................137 Options ................................................................................................................................142 Deleting/Destroying a Record ...................................................................................................142 destroy .................................................................................................................................143 delete ...................................................................................................................................146 Transactions ..............................................................................................................................147 Scopes ......................................................................................................................................148 Preparation ..........................................................................................................................148 Defining a Scope .................................................................................................................149 Passing In Arguments ..........................................................................................................151 Creating New Records with Scopes ....................................................................................151 Validation ..................................................................................................................................152 Preparation ..........................................................................................................................152 The Basic Idea .....................................................................................................................152 valid? ...................................................................................................................................154 presence ..............................................................................................................................156 length ..................................................................................................................................157 numericality .........................................................................................................................159 uniqueness ..........................................................................................................................161 inclusion ..............................................................................................................................163 exclusion .............................................................................................................................165 format ..................................................................................................................................165 General Validation Options ...................................................................................................166 Writing Custom Validations ..................................................................................................167 Further Documentation ........................................................................................................169 Migrations .................................................................................................................................169 Which Database Is Used? ....................................................................................................173 Creating Index .....................................................................................................................176 Automatically Added Fields (id, created_at, and updated_at) .............................................177 Further Documentation ........................................................................................................178 ix Table of ConTenTs Callbacks...................................................................................................................................178 Default Values ...........................................................................................................................180 Chapter 4: Scaffolding and REST ��������������������������������������������������������������������������183 Redirects and Flash Messages .................................................................................................183 Redirects .............................................................................................................................183 redirect_to :back .................................................................................................................186 Flash Messages ...................................................................................................................187 Different Types of Flash Messages ......................................................................................189 Why Are There Flash Messages at All? ................................................................................190 Generating a Scaffold ...............................................................................................................190 The Routes ...........................................................................................................................192 The Controller ......................................................................................................................193 The Views ............................................................................................................................199 When Should You Use Scaffolding? ..........................................................................................211 Example for a Minimal Project ............................................................................................211 Conclusion ...........................................................................................................................216 Chapter 5: Routes �������������������������������������������������������������������������������������������������217 HTTP GET Requests for Singular Resources .............................................................................218 Naming a Route ...................................................................................................................220 as .........................................................................................................................................220 to .........................................................................................................................................220 Parameters ..........................................................................................................................221 Constraints ..........................................................................................................................225 Redirects .............................................................................................................................228 root :to ⇒ welcome#index ........................................................................................................229 resources ..................................................................................................................................229 Selecting Specific Routes with only: or except: ...................................................................230 Nested Resources ................................................................................................................232 Further Information on Routes ..................................................................................................242 x

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.