Ruby on Rails
Total Page:16
File Type:pdf, Size:1020Kb
Ruby on Rails Wikibooks.org December 1, 2012 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. An URI to this license is given in the list of figures on page 91. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 89. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 95, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 91. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, we recommend the use of http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility or clicking the paper clip attachment symbol on the lower left of your PDF Viewer, selecting Save Attachment. After extracting it from the PDF file you have to rename it to source.7z. To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX source itself was generated by a program written by Dirk HÃijnniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. This distribution also contains a configured version of the pdflatex compiler with all necessary packages and fonts needed to compile the LATEX source included in this PDF file. Contents 1 Introduction . 3 2 Getting Started . 5 2.1 Installation . 5 2.2 Installation on Windows . 5 2.3 Install on OS X . 7 2.4 Install on Linux . 8 2.5 Concepts . 9 2.6 Don´t repeat yourself . 9 2.7 Model-View-Controller . 9 2.8 Convention over Configuration . 11 2.9 First Application . 11 2.10 First Application . 11 2.11 Running the Rails server . 17 3 Built-In Rails Tools . 19 3.1 Generators . 19 3.2 Generators . 19 3.3 Rake . 22 3.4 Database Tasks . 22 3.5 Test Tasks . 22 3.6 Cache Tasks . 22 4 ActiveRecord - The Model . 25 4.1 Naming . 25 4.2 Migrations . 26 4.3 Associations . 31 4.4 Callbacks . 36 4.5 Validations . 40 4.6 Attributes . 43 5 > #<Product: id . 45 5.1 Aggregations . 46 5.2 Calculations . 48 6 ActionView - The View . 53 6.1 Rendering and Redirecting . 53 6.2 Layout Files . 55 6.3 Forms . 57 6.4 Uploads . 62 6.5 Custom Helpers . 62 7 ActionController - The Controller . 65 7.1 Introduction . 65 7.2 Actions . 65 7.3 Parameters . 66 III Contents 7.4 Session . 66 7.5 Cookies . 67 8 Routing . 69 8.1 Understanding Routes and Routing . 69 8.2 See also . 73 9 ActiveSupport . 75 10 ActionMailer . 77 10.1 Model . 77 10.2 Mailer Views . 78 10.3 Sending HTML Mail . 78 10.4 Multipart Mail . 78 10.5 Attachments . 79 10.6 Configuration Options . 80 11 Examples . 81 11.1 Step By Step . 81 11.2 Find . 81 11.3 Rake . 82 11.4 Server . 83 11.5 Fixing Errors . 83 11.6 Shell Commands . 83 12 Other Resources . 85 12.1 Plugins . 85 12.2 Websites . 85 12.3 Books . 86 12.4 Development Tools . 86 12.5 Weblogs . 87 12.6 Mailing List . 87 12.7 Freenode ROR Channel . 87 13 Contributors . 89 List of Figures . 91 14 Licenses . 95 14.1 GNU GENERAL PUBLIC LICENSE . 95 14.2 GNU Free Documentation License . 96 14.3 GNU Lesser General Public License . 96 Note: the current version of this book can be found at http://en.wikibooks.org/wiki/ Ruby_on_Railshttp:// 1 1 Introduction Ruby on Rails, or often seen as RoR or just Rails, is an web application framework written in the programming language Ruby. Rails was created to be a project management tool for " 37signals1". Quickly, it became one of the most popular frameworks on the web and its ideas and philosophies has inspired many frameworks in different languages. 1.0.1 Features • Rails supports agile software development2 • Rails has a short and very intuitive language • Single blocks of code are intended to be short and clear • Rails is very flexible • Rails comes with a testing framework • The framework follows the principles of DRY3 (Don't repeat yourself) • Rails has some conventions: these allow quick development and consistent code • Rails uses the MVC-Architecture4 (Model-View-Controller) • Rails comes with an integrated server for local testing • Applications with Rails are RESTful5 1 http://www.37signals.com 2 http://en.wikipedia.org/wiki/Agile_development 3 http://en.wikipedia.org/wiki/Don%27t_repeat_yourself 4 http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 5 http://en.wikipedia.org/wiki/Representational_State_Transfer 3 2 Getting Started 2.1 Installation 2.2 Installation on Windows To start, you will need the following components: • Ruby • RubyGems • Rails • a driver for your database 2.2.1 Ruby Install Ruby as a regular application. You might need administrator privileges to install it successfully. Check the Ruby site.