From 7fd790e6820c71dd0e6b2dd52d8a5c220dd1002c Mon Sep 17 00:00:00 2001 From: Matthew Daubert Date: Tue, 13 Mar 2012 20:50:39 -0400 Subject: [PATCH 01/28] Minor clarification in documentation language and spelling --- actionpack/lib/action_view/template/resolver.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 7fa86866a71ee..8ea2e5bfe4f0f 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -176,7 +176,7 @@ def extract_handler_and_format(path, default_formats) end end - # A resolver that loads files from the filesystem. It allows to set your own + # A resolver that loads files from the filesystem. It allows setting your own # resolving pattern. Such pattern can be a glob string supported by some variables. # # ==== Examples @@ -192,7 +192,7 @@ def extract_handler_and_format(path, default_formats) # # FileSystemResolver.new("/path/to/views", ":prefix/{:formats/,}:action{.:locale,}{.:formats,}{.:handlers,}") # - # If you don't specify pattern then the default will be used. + # If you don't specify a pattern then the default will be used. # # In order to use any of the customized resolvers above in a Rails application, you just need # to configure ActionController::Base.view_paths in an initializer, for example: @@ -204,10 +204,10 @@ def extract_handler_and_format(path, default_formats) # # ==== Pattern format and variables # - # Pattern have to be a valid glob string, and it allows you to use the + # Pattern has to be a valid glob string, and it allows you to use the # following variables: # - # * :prefix - usualy the controller path + # * :prefix - usually the controller path # * :action - name of the action # * :locale - possible locale versions # * :formats - possible request formats (for example html, json, xml...) From dcfb990e1bd56df44595782bd0fe356e6c8f2c76 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:31:38 -0700 Subject: [PATCH 02/28] Move database configuration section from Getting Started Guide into Configuration guide This is because newbies don't need to know immediately all the different ways of configuring a database on Rails. The default is SQLite3 which'll work on most operating systems by default. The only reason for it to *not* work is due to missing packages on the operating system, which should be taken care of in some sort of 'Installing Rails for guide. --- railties/guides/source/configuring.textile | 93 ++++++++++++ .../guides/source/getting_started.textile | 140 ------------------ 2 files changed, 93 insertions(+), 140 deletions(-) diff --git a/railties/guides/source/configuring.textile b/railties/guides/source/configuring.textile index d6e500fc4b6eb..28d198c00ba02 100644 --- a/railties/guides/source/configuring.textile +++ b/railties/guides/source/configuring.textile @@ -467,6 +467,99 @@ There are a few configuration options available in Active Support: * +ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+. +h4. Configuring a Database + +Just about every Rails application will interact with a database. The database to use is specified in a configuration file called +config/database.yml+. If you open this file in a new Rails application, you'll see a default database configured to use SQLite3. The file contains sections for three different environments in which Rails can run by default: + +* The +development+ environment is used on your development/local computer as you interact manually with the application. +* The +test+ environment is used when running automated tests. +* The +production+ environment is used when you deploy your application for the world to use. + +TIP: You don't have to update the database configurations manually. If you look at the options of the application generator, you will see that one of the options is named --database. This option allows you to choose an adapter from a list of the most used relational databases. You can even run the generator repeatedly: cd .. && rails new blog --database=mysql. When you confirm the overwriting of the +config/database.yml+ file, your application will be configured for MySQL instead of SQLite. Detailed examples of the common database connections are below. + +h5. Configuring an SQLite3 Database + +Rails comes with built-in support for "SQLite3":http://www.sqlite.org, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using an SQLite database when creating a new project, but you can always change it later. + +Here's the section of the default configuration file (config/database.yml) with connection information for the development environment: + + +development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 5 + timeout: 5000 + + +NOTE: Rails uses an SQLite3 database for data storage by default because it is a zero configuration database that just works. Rails also supports MySQL and PostgreSQL "out of the box", and has plugins for many database systems. If you are using a database in a production environment Rails most likely has an adapter for it. + +h5. Configuring a MySQL Database + +If you choose to use MySQL instead of the shipped SQLite3 database, your +config/database.yml+ will look a little different. Here's the development section: + + +development: + adapter: mysql2 + encoding: utf8 + database: blog_development + pool: 5 + username: root + password: + socket: /tmp/mysql.sock + + +If your development computer's MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the +development+ section as appropriate. + +h5. Configuring a PostgreSQL Database + +If you choose to use PostgreSQL, your +config/database.yml+ will be customized to use PostgreSQL databases: + + +development: + adapter: postgresql + encoding: unicode + database: blog_development + pool: 5 + username: blog + password: + + +h5. Configuring an SQLite3 Database for JRuby Platform + +If you choose to use SQLite3 and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section: + + +development: + adapter: jdbcsqlite3 + database: db/development.sqlite3 + + +h5. Configuring a MySQL Database for JRuby Platform + +If you choose to use MySQL and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section: + + +development: + adapter: jdbcmysql + database: blog_development + username: root + password: + + +h5. Configuring a PostgreSQL Database for JRuby Platform + +If you choose to use PostgreSQL and are using JRuby, your +config/database.yml+ will look a little different. Here's the development section: + + +development: + adapter: jdbcpostgresql + encoding: unicode + database: blog_development + username: blog + password: + + +Change the username and password in the +development+ section as appropriate. h3. Rails Environment Settings diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index bed14ef6a81e9..dd1dd55f56da9 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -292,146 +292,6 @@ rundown on the function of each of the files and folders that Rails created by d |tmp/|Temporary files| |vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems and the Rails source code (if you optionally install it into your project).| -h4. Configuring a Database - -Just about every Rails application will interact with a database. The database -to use is specified in a configuration file, +config/database.yml+. If you open -this file in a new Rails application, you'll see a default database -configured to use SQLite3. The file contains sections for three different -environments in which Rails can run by default: - -* The +development+ environment is used on your development/local computer as you interact -manually with the application. -* The +test+ environment is used when running automated tests. -* The +production+ environment is used when you deploy your application for the world to use. - -TIP: You don't have to update the database configurations manually. If you look at the -options of the application generator, you will see that one of the options -is named --database. This option allows you to choose an adapter from a -list of the most used relational databases. You can even run the generator -repeatedly: cd .. && rails new blog --database=mysql. When you confirm the overwriting - of the +config/database.yml+ file, your application will be configured for MySQL -instead of SQLite. Detailed examples of the common database connections are below. - -h5. Configuring an SQLite3 Database - -Rails comes with built-in support for "SQLite3":http://www.sqlite.org, which is -a lightweight serverless database application. While a busy production -environment may overload SQLite, it works well for development and testing. -Rails defaults to using an SQLite database when creating a new project, but you -can always change it later. - -Here's the section of the default configuration file -(config/database.yml) with connection information for the development -environment: - - -development: - adapter: sqlite3 - database: db/development.sqlite3 - pool: 5 - timeout: 5000 - - -NOTE: In this guide we are using an SQLite3 database for data storage, because -it is a zero configuration database that just works. Rails also supports MySQL -and PostgreSQL "out of the box", and has plugins for many database systems. If -you are using a database in a production environment Rails most likely has an -adapter for it. - -h5. Configuring a MySQL Database - -If you choose to use MySQL instead of the shipped SQLite3 database, your -+config/database.yml+ will look a little different. Here's the development -section: - - -development: - adapter: mysql2 - encoding: utf8 - database: blog_development - pool: 5 - username: root - password: - socket: /tmp/mysql.sock - - -If your development computer's MySQL installation includes a root user with an -empty password, this configuration should work for you. Otherwise, change the -username and password in the +development+ section as appropriate. - -h5. Configuring a PostgreSQL Database - -If you choose to use PostgreSQL, your +config/database.yml+ will be customized -to use PostgreSQL databases: - - -development: - adapter: postgresql - encoding: unicode - database: blog_development - pool: 5 - username: blog - password: - - -h5. Configuring an SQLite3 Database for JRuby Platform - -If you choose to use SQLite3 and are using JRuby, your +config/database.yml+ will -look a little different. Here's the development section: - - -development: - adapter: jdbcsqlite3 - database: db/development.sqlite3 - - -h5. Configuring a MySQL Database for JRuby Platform - -If you choose to use MySQL and are using JRuby, your +config/database.yml+ will look -a little different. Here's the development section: - - -development: - adapter: jdbcmysql - database: blog_development - username: root - password: - - -h5. Configuring a PostgreSQL Database for JRuby Platform - -Finally if you choose to use PostgreSQL and are using JRuby, your -+config/database.yml+ will look a little different. Here's the development -section: - - -development: - adapter: jdbcpostgresql - encoding: unicode - database: blog_development - username: blog - password: - - -Change the username and password in the +development+ section as appropriate. - -h4. Creating the Database - -Now that you have your database configured, it's time to have Rails create an -empty database for you. You can do this by running a rake command: - - -$ rake db:create - - -This will create your development and test SQLite3 databases inside the -db/ folder. - -TIP: Rake is a general-purpose command-runner that Rails uses for many things. -You can see the list of available rake commands in your application by running -+rake -T+. - h3. Hello, Rails! One of the traditional places to start with a new language is by getting some From 2f06c94e38a116fdfa43d7b7117e6bf911a0bff5 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:32:21 -0700 Subject: [PATCH 03/28] [getting started] Remove super-early mention of REST from Getting Started guide. We will mention this as we introduce the routing components for Rails later on in the guide. --- .../guides/source/getting_started.textile | 149 +----------------- 1 file changed, 1 insertion(+), 148 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index dd1dd55f56da9..0fce4beae0765 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -61,158 +61,11 @@ tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience. -The Rails philosophy includes several guiding principles: +The Rails philosophy includes two major guiding principles: * DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing. * Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than requiring you to specify every little thing through endless configuration files. -* REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs -is the fastest way to go. - -h4. The MVC Architecture - -At the core of Rails is the Model, View, Controller architecture, usually just -called MVC. MVC benefits include: - -* Isolation of business logic from the user interface -* Ease of keeping code DRY -* Making it clear where different types of code belong for easier maintenance - -h5. Models - -A model represents the information (data) of the application and the rules to -manipulate that data. In the case of Rails, models are primarily used for -managing the rules of interaction with a corresponding database table. In most -cases, each table in your database will correspond to one model in your -application. The bulk of your application's business logic will be concentrated -in the models. - -h5. Views - -Views represent the user interface of your application. In Rails, views are -often HTML files with embedded Ruby code that perform tasks related solely to -the presentation of the data. Views handle the job of providing data to the web -browser or other tool that is used to make requests from your application. - -h5. Controllers - -Controllers provide the "glue" between models and views. In Rails, controllers -are responsible for processing the incoming requests from the web browser, -interrogating the models for data, and passing that data on to the views for -presentation. - -h4. The Components of Rails - -Rails ships as many individual components. Each of these components are briefly -explained below. If you are new to Rails, as you read this section, don't get -hung up on the details of each component, as they will be explained in further -detail later. For instance, we will bring up Rack applications, but you don't -need to know anything about them to continue with this guide. - -* Action Pack - ** Action Controller - ** Action Dispatch - ** Action View -* Action Mailer -* Active Model -* Active Record -* Active Resource -* Active Support -* Railties - -h5. Action Pack - -Action Pack is a single gem that contains Action Controller, Action View and -Action Dispatch. The "VC" part of "MVC". - -h6. Action Controller - -Action Controller is the component that manages the controllers in a Rails -application. The Action Controller framework processes incoming requests to a -Rails application, extracts parameters, and dispatches them to the intended -action. Services provided by Action Controller include session management, -template rendering, and redirect management. - -h6. Action View - -Action View manages the views of your Rails application. It can create both HTML -and XML output by default. Action View manages rendering templates, including -nested and partial templates, and includes built-in AJAX support. View -templates are covered in more detail in another guide called "Layouts and -Rendering":layouts_and_rendering.html. - -h6. Action Dispatch - -Action Dispatch handles routing of web requests and dispatches them as you want, -either to your application or any other Rack application. Rack applications are -a more advanced topic and are covered in a separate guide called "Rails on -Rack":rails_on_rack.html. - -h5. Action Mailer - -Action Mailer is a framework for building e-mail services. You can use Action -Mailer to receive and process incoming email and send simple plain text or -complex multipart emails based on flexible templates. - -h5. Active Model - -Active Model provides a defined interface between the Action Pack gem services -and Object Relationship Mapping gems such as Active Record. Active Model allows -Rails to utilize other ORM frameworks in place of Active Record if your -application needs this. - -h5. Active Record - -Active Record is the base for the models in a Rails application. It provides -database independence, basic CRUD functionality, advanced finding capabilities, -and the ability to relate models to one another, among other services. - -h5. Active Resource - -Active Resource provides a framework for managing the connection between -business objects and RESTful web services. It implements a way to map web-based -resources to local objects with CRUD semantics. - -h5. Active Support - -Active Support is an extensive collection of utility classes and standard Ruby -library extensions that are used in Rails, both by the core code and by your -applications. - -h5. Railties - -Railties is the core Rails code that builds new Rails applications and glues the -various frameworks and plugins together in any Rails application. - -h4. REST - -Rest stands for Representational State Transfer and is the foundation of the -RESTful architecture. This is generally considered to be Roy Fielding's doctoral -thesis, "Architectural Styles and the Design of Network-based Software -Architectures":http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm. While -you can read through the thesis, REST in terms of Rails boils down to two main -principles: - -* Using resource identifiers such as URLs to represent resources. -* Transferring representations of the state of that resource between system components. - -For example, the following HTTP request: - -DELETE /photos/17 - -would be understood to refer to a photo resource with the ID of 17, and to -indicate a desired action - deleting that resource. REST is a natural style for -the architecture of web applications, and Rails hooks into this shielding you -from many of the RESTful complexities and browser quirks. - -If you'd like more details on REST as an architectural style, these resources -are more approachable than Fielding's thesis: - -* "A Brief Introduction to REST":http://www.infoq.com/articles/rest-introduction by Stefan Tilkov -* "An Introduction to REST":http://bitworking.org/news/373/An-Introduction-to-REST (video tutorial) by Joe Gregorio -* "Representational State Transfer":http://en.wikipedia.org/wiki/Representational_State_Transfer article in Wikipedia -* "How to GET a Cup of Coffee":http://www.infoq.com/articles/webber-rest-workflow by Jim Webber, Savas Parastatidis & -Ian Robinson h3. Creating a New Rails Project From 5cbdd0f667f9c6f078a01f4319bb97186c70fda7 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:32:50 -0700 Subject: [PATCH 04/28] [getting started] *THE* way to install Rails is by running +gem install+. You should not run this as the root user. --- railties/guides/source/getting_started.textile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 0fce4beae0765..29c9e8dde1ff8 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -81,10 +81,9 @@ TIP: The examples below use # and $ to denote terminal prompts. If you are using h4. Installing Rails -In most cases, the easiest way to install Rails is to take advantage of RubyGems: +To install Rails, use the +gem install+ command provided by RubyGems: -Usually run this as the root user: # gem install rails From 5013f51348ee8467e88a74d43da167c2d5d85b39 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:33:43 -0700 Subject: [PATCH 05/28] [getting started] update Rails version to 3.2.2 --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 29c9e8dde1ff8..064e88da0a332 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -97,7 +97,7 @@ the following: $ rails --version -If it says something like "Rails 3.1.3" you are ready to continue. +If it says something like "Rails 3.2.2" you are ready to continue. h4. Creating the Blog Application From 58bf4c45ec3bd39e7cb2ae5a4ec425e87f095475 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:34:15 -0700 Subject: [PATCH 06/28] [getting started] fix lines that are too short. This should be a configuration setting on your text editor, rather than hard-coded into the guide --- railties/guides/source/getting_started.textile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 064e88da0a332..8c131b2d70dc8 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -121,11 +121,7 @@ directly in that application: $ cd blog -The 'rails new blog' command we ran above created a folder in your working directory -called blog. The blog folder has a number of auto-generated folders -that make up the structure of a Rails application. Most of the work in -this tutorial will happen in the app/ folder, but here's a basic -rundown on the function of each of the files and folders that Rails created by default: +The +rails new blog+ command we ran above created a folder in your working directory called blog. The blog directory has a number of auto-generated folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the app/ folder, but here's a basic rundown on the function of each of the files and folders that Rails created by default: |_.File/Folder|_.Purpose| |app/|Contains the controllers, models, views and assets for your application. You'll focus on this folder for the remainder of this guide.| From 7b2bd23b29954db50782a7e451104eb525374f74 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:37:56 -0700 Subject: [PATCH 07/28] [getting started] more line lengthening --- .../guides/source/getting_started.textile | 81 +++++-------------- 1 file changed, 18 insertions(+), 63 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 8c131b2d70dc8..d2ca60f8d6ee3 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -87,11 +87,9 @@ To install Rails, use the +gem install+ command provided by RubyGems: # gem install rails -TIP. If you're working on Windows, you can quickly install Ruby and Rails with -"Rails Installer":http://railsinstaller.org. +TIP. If you're working on Windows, you can quickly install Ruby and Rails with "Rails Installer":http://railsinstaller.org. -To verify that you have everything installed correctly, you should be able to run -the following: +To verify that you have everything installed correctly, you should be able to run the following: $ rails --version @@ -101,8 +99,7 @@ If it says something like "Rails 3.2.2" you are ready to continue. h4. Creating the Blog Application -To begin, open a terminal, navigate to a folder where you have rights to create -files, and type: +To begin, open a terminal, navigate to a folder where you have rights to create files, and type: $ rails new blog @@ -110,12 +107,9 @@ $ rails new blog This will create a Rails application called Blog in a directory called blog. -TIP: You can see all of the switches that the Rails application builder accepts -by running -rails new -h. +TIP: You can see all of the switches that the Rails application builder accepts by running rails new -h. -After you create the blog application, switch to its folder to continue work -directly in that application: +After you create the blog application, switch to its folder to continue work directly in that application: $ cd blog @@ -142,61 +136,37 @@ The +rails new blog+ command we ran above created a folder in your working direc h3. Hello, Rails! -One of the traditional places to start with a new language is by getting some -text up on screen quickly. To do this, you need to get your Rails application -server running. +One of the traditional places to start with a new language is by getting some text up on screen quickly. To do this, you need to get your Rails application server running. h4. Starting up the Web Server -You actually have a functional Rails application already. To see it, you need to -start a web server on your development machine. You can do this by running: +You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running: $ rails server -TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and -the absence of a runtime will give you an +execjs+ error. Usually Mac OS X -and Windows come with a JavaScript runtime installed. Rails adds the +therubyracer+ gem -to Gemfile in a commented line for new apps and you can uncomment if you need it. -+therubyrhino+ is the recommended runtime for JRuby users and is added by default -to Gemfile in apps generated under JRuby. You can investigate about all the -supported runtimes at "ExecJS":https://github.com/sstephenson/execjs#readme. +TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an +execjs+ error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the +therubyracer+ gem to Gemfile in a commented line for new apps and you can uncomment if you need it. +therubyrhino+ is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at "ExecJS":https://github.com/sstephenson/execjs#readme. -This will fire up an instance of the WEBrick web server by default (Rails can -also use several other web servers). To see your application in action, open a -browser window and navigate to "http://localhost:3000":http://localhost:3000. -You should see Rails' default information page: +This will fire up an instance of a webserver built into Ruby called WEBrick by default. To see your application in action, open a browser window and navigate to "http://localhost:3000":http://localhost:3000. You should see Rails' default information page: !images/rails_welcome.png(Welcome Aboard screenshot)! -TIP: To stop the web server, hit Ctrl+C in the terminal window where it's -running. In development mode, Rails does not generally require you to stop the -server; changes you make in files will be automatically picked up by the server. +TIP: To stop the web server, hit Ctrl+C in the terminal window where it's running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server. -The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it -makes sure that you have your software configured correctly enough to serve a -page. You can also click on the _About your application’s environment_ link to -see a summary of your application's environment. +The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. You can also click on the _About your application’s environment_ link to see a summary of your application's environment. h4. Say "Hello", Rails -To get Rails saying "Hello", you need to create at minimum a controller and a -view. Fortunately, you can do that in a single command. Enter this command in -your terminal: +To get Rails saying "Hello", you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal: $ rails generate controller home index -TIP: If you get a command not found error when running this command, you -need to explicitly pass Rails +rails+ commands to Ruby: ruby -\path\to\your\application\script\rails generate controller home index. +Rails will create several files for you, including +app/views/home/index.html.erb+. This is the template that will be used to display the results of the +index+ action (method) in the +home+ controller. -Rails will create several files for you, including -+app/views/home/index.html.erb+. This is the template that will be used to -display the results of the +index+ action (method) in the +home+ controller. -Open this file in your text editor and edit it to contain a single line of code: +Open the +app/views/home/index/html.erb+ file in your text editor and edit it to contain a single line of code:

Hello, Rails!

@@ -204,29 +174,14 @@ Open this file in your text editor and edit it to contain a single line of code: h4. Setting the Application Home Page -Now that we have made the controller and view, we need to tell Rails when we -want "Hello Rails!" to show up. In our case, we want it to show up when we -navigate to the root URL of our site, -"http://localhost:3000":http://localhost:3000, instead of the "Welcome Aboard" -smoke test. +Now that we have made the controller and view, we need to tell Rails when we want "Hello Rails!" to show up. In our case, we want it to show up when we navigate to the root URL of our site, "http://localhost:3000":http://localhost:3000. At the moment, however, the "Welcome Aboard" smoke test is occupying that spot. -The first step to doing this is to delete the default page from your -application: +To fix this, delete the +index.html+ file located inside the +public+ directory of the application. - -$ rm public/index.html - -We need to do this as Rails will deliver any static file in the +public+ -directory in preference to any dynamic content we generate from the controllers. +We need to do this as Rails will deliver any static file in the +public+ directory in preference to any dynamic content we generate from the controllers. -Now, you have to tell Rails where your actual home page is located. Open the -file +config/routes.rb+ in your editor. This is your application's _routing -file_ which holds entries in a special DSL (domain-specific language) that tells -Rails how to connect incoming requests to controllers and actions. This file -contains many sample routes on commented lines, and one of them actually shows -you how to connect the root of your site to a specific controller and action. -Find the line beginning with +root :to+ and uncomment it. It should look something like the following: +Now, you have to tell Rails where your actual home page is located. Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with +root :to+ and uncomment it. It should look something like the following: Blog::Application.routes.draw do From 329db6ff6ccee1608979fed74aaf239c98f50b30 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 11:51:29 -0700 Subject: [PATCH 08/28] [getting started] briefly explain what a controller and view are --- railties/guides/source/getting_started.textile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index d2ca60f8d6ee3..249bf67f57735 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -158,7 +158,14 @@ The "Welcome Aboard" page is the _smoke test_ for a new Rails application: it ma h4. Say "Hello", Rails -To get Rails saying "Hello", you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal: +To get Rails saying "Hello", you need to create at minimum a _controller_ and a _view_. + +A controller's purpose is to receive specific requests for the application. What controller receives what request is determined by the _routing_. There is very often more than one route to each controller, and different routes can be served by different _actions_. Each action's purpose is to collect information to provide it to a view. + +A view's purpose is to display this information in a human readable format. An important distinction to make is that it is the _controller_, not the view, where information is collected. The view should just display that information. By default, view templates are written in a language called ERB (Embedded Ruby) which is converted by the request cycle in Rails before being sent to the user. + + +To create a new controller, run this command: $ rails generate controller home index From f7a37717448c733aefd37fe633ac56447b180319 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 12:00:32 -0700 Subject: [PATCH 09/28] [getting started] mention generators early on, when rails new is mentioned. This is to show one of the 'wins' of Rails nice and early. More are demonstrated as we flow through the guide. --- railties/guides/source/getting_started.textile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 249bf67f57735..51969d2eac0ca 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -99,7 +99,9 @@ If it says something like "Rails 3.2.2" you are ready to continue. h4. Creating the Blog Application -To begin, open a terminal, navigate to a folder where you have rights to create files, and type: +Rails comes with a number of generators that are designed to make your development life easier. One of these is the new application generator, which will provide you with the foundation of a Rails application so that you don't have to write it yourself. + +To use this generator, open a terminal, navigate to a directory where you have rights to create files, and type: $ rails new blog @@ -164,14 +166,13 @@ A controller's purpose is to receive specific requests for the application. What A view's purpose is to display this information in a human readable format. An important distinction to make is that it is the _controller_, not the view, where information is collected. The view should just display that information. By default, view templates are written in a language called ERB (Embedded Ruby) which is converted by the request cycle in Rails before being sent to the user. - To create a new controller, run this command: $ rails generate controller home index -Rails will create several files for you, including +app/views/home/index.html.erb+. This is the template that will be used to display the results of the +index+ action (method) in the +home+ controller. +Rails will create several files for you. Most important of these are of course the controller, located at +app/controllers/home_controller.rb+ and the view, located at +app/views/home/index.html.erb+. Open the +app/views/home/index/html.erb+ file in your text editor and edit it to contain a single line of code: From 4c1861f427f47d95203e7796bca655bceaf63df0 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 12:00:54 -0700 Subject: [PATCH 10/28] [getting started] Improve prose for initial routing section --- railties/guides/source/getting_started.textile | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 51969d2eac0ca..baf14b4f5ddfc 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -186,10 +186,11 @@ Now that we have made the controller and view, we need to tell Rails when we wan To fix this, delete the +index.html+ file located inside the +public+ directory of the application. +We need to do this because Rails will serve any static file in the +public+ directory that matches a route in preference to any dynamic content we generate from the controllers. -We need to do this as Rails will deliver any static file in the +public+ directory in preference to any dynamic content we generate from the controllers. +Next, you have to tell Rails where your actual home page is located. -Now, you have to tell Rails where your actual home page is located. Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with +root :to+ and uncomment it. It should look something like the following: +Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with +root :to+ and uncomment it. It should look something like the following: Blog::Application.routes.draw do @@ -200,11 +201,9 @@ Blog::Application.routes.draw do root :to => "home#index" -The +root :to => "home#index"+ tells Rails to map the root action to the home -controller's index action. +The +root :to => "home#index"+ tells Rails to map requests to the root of the application to the home controller's index action. This was created earlier when you ran the controller generator (+rails generate controller home index+). -Now if you navigate to "http://localhost:3000":http://localhost:3000 in your -browser, you'll see +Hello, Rails!+. +If you navigate to "http://localhost:3000":http://localhost:3000 in your browser, you'll see +Hello, Rails!+. NOTE. For more information about routing, refer to "Rails Routing from the Outside In":routing.html. From c71b9612c0dde4146bee86679e6319a913c24834 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 12:02:52 -0700 Subject: [PATCH 11/28] [getting started] Link to Bundler website when explaining Gemfile[.lock] --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index baf14b4f5ddfc..39e7a387ace4b 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -125,7 +125,7 @@ The +rails new blog+ command we ran above created a folder in your working direc |config.ru|Rack configuration for Rack based servers used to start the application.| |db/|Contains your current database schema, as well as the database migrations.| |doc/|In-depth documentation for your application.| -|Gemfile
Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application.| +|Gemfile
Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see "the Bundler website":http://gembundler.com | |lib/|Extended modules for your application.| |log/|Application log files.| |public/|The only folder seen to the world as-is. Contains the static files and compiled assets.| From 66e5e7136d2e9ddeba790268e4cf019f3814436f Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 14:32:31 -0700 Subject: [PATCH 12/28] [getting started] add warning that guide is being worked on (just for edge) --- railties/guides/source/getting_started.textile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 39e7a387ace4b..81e8a68835764 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -13,6 +13,8 @@ endprologue. WARNING. This Guide is based on Rails 3.1. Some of the code shown here will not work in earlier versions of Rails. +WARNING: The Edge version of this guide is currently being re-worked. Please excuse us while we re-arrange the place. + h3. Guide Assumptions This guide is designed for beginners who want to get started with a Rails From 35e0cb51f839974fa95c569d016f7141f78dcee6 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 14:33:16 -0700 Subject: [PATCH 13/28] [getting started] Default root route in config/routes.rb is for a welcome controller. Let's make it easy by creating a WelcomeController, not a HomeController, in the guide --- railties/guides/source/getting_started.textile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 81e8a68835764..8e3fa92c65b94 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -171,12 +171,12 @@ A view's purpose is to display this information in a human readable format. An i To create a new controller, run this command: -$ rails generate controller home index +$ rails generate controller welcome index -Rails will create several files for you. Most important of these are of course the controller, located at +app/controllers/home_controller.rb+ and the view, located at +app/views/home/index.html.erb+. +Rails will create several files for you. Most important of these are of course the controller, located at +app/controllers/welcome_controller.rb+ and the view, located at +app/views/welcome/index.html.erb+. -Open the +app/views/home/index/html.erb+ file in your text editor and edit it to contain a single line of code: +Open the +app/views/welcome/index/html.erb+ file in your text editor and edit it to contain a single line of code:

Hello, Rails!

@@ -317,7 +317,7 @@ invoking the command: rake db:migrate RAILS_ENV=production. h4. Adding a Link To hook the posts up to the home page you've already created, you can add a link -to the home page. Open +app/views/home/index.html.erb+ and modify it as follows: +to the home page. Open +app/views/welcome/index.html.erb+ and modify it as follows:

Hello, Rails!

@@ -904,7 +904,7 @@ Associations":association_basics.html guide. h4. Adding a Route for Comments -As with the +home+ controller, we will need to add a route so that Rails knows +As with the +welcome+ controller, we will need to add a route so that Rails knows where we would like to navigate to see +comments+. Open up the +config/routes.rb+ file again. Near the top, you will see the entry for +posts+ that was added automatically by the scaffold generator: resources From 7dabf57f9e3fee0b208041c864dc3194ce47c872 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 14:34:50 -0700 Subject: [PATCH 14/28] [getting started] Reworking beginnings of the 'meat' of the guide This guide will now create the new action first, then the create action and so on and so forth until the controller has all seven RESTful actions. We are doing this to demonstrate to a user how to *properly* build a controller. We do not mention scaffold *ON PURPOSE*. Scaffold is evil. We want to show the users the different facets of creating a resource, and this is how we're going to do it. --- .../guides/source/getting_started.textile | 75 ++++++++----------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 8e3fa92c65b94..c0af3cdc08c17 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -200,62 +200,49 @@ Blog::Application.routes.draw do #... # You can have the root of your site routed with "root" # just remember to delete public/index.html. - root :to => "home#index" + root :to => "welcome#index"
-The +root :to => "home#index"+ tells Rails to map requests to the root of the application to the home controller's index action. This was created earlier when you ran the controller generator (+rails generate controller home index+). +The +root :to => "welcome#index"+ tells Rails to map requests to the root of the application to the welcome controller's index action. This was created earlier when you ran the controller generator (+rails generate controller welcome index+). If you navigate to "http://localhost:3000":http://localhost:3000 in your browser, you'll see +Hello, Rails!+. -NOTE. For more information about routing, refer to "Rails Routing from the -Outside In":routing.html. +NOTE. For more information about routing, refer to "Rails Routing from the Outside In":routing.html. -h3. Getting Up and Running Quickly with Scaffolding +h3. Getting Up and Running -Rails _scaffolding_ is a quick way to generate some of the major pieces of an -application. If you want to create the models, views, and controllers for a new -resource in a single operation, scaffolding is the tool for the job. +Now that you've seen how to create a controller, an action and a view, let's create something with a bit more substance. -h3. Creating a Resource +In the Blog application, you will now create a new _resource_. A resource is the term used for a collection of similar objects, such as posts, people or animals. You can create, read, update and destroy items for a resource and these operations are referred to as _CRUD_ operations. -In the case of the blog application, you can start by generating a scaffold for the -Post resource: this will represent a single blog posting. To do this, enter this -command in your terminal: +In the next section, you will add the ability to create new posts in your application and be able to view them. This is the "CR" from CRUD. The form for doing this will look like this: - -$ rails generate scaffold Post name:string title:string content:text - +!images/getting_started/new_post.png(The new post form)! -The scaffold generator will build several files in your application, along with some -folders, and edit config/routes.rb. Here's a quick overview of what it creates: +It will look a little basic for now, but that's ok. We'll look at improving the styling for it afterwards. -|_.File |_.Purpose| -|db/migrate/20100207214725_create_posts.rb |Migration to create the posts table in your database (your name will include a different timestamp)| -|app/models/post.rb |The Post model| -|test/unit/post_test.rb |Unit testing harness for the posts model| -|test/fixtures/posts.yml |Sample posts for use in testing| -|config/routes.rb |Edited to include routing information for posts| -|app/controllers/posts_controller.rb |The Posts controller| -|app/views/posts/index.html.erb |A view to display an index of all posts | -|app/views/posts/edit.html.erb |A view to edit an existing post| -|app/views/posts/show.html.erb |A view to display a single post| -|app/views/posts/new.html.erb |A view to create a new post| -|app/views/posts/_form.html.erb |A partial to control the overall look and feel of the form used in edit and new views| -|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller| -|app/helpers/posts_helper.rb |Helper functions to be used from the post views| -|test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper| -|app/assets/javascripts/posts.js.coffee |CoffeeScript for the posts controller| -|app/assets/stylesheets/posts.css.scss |Cascading style sheet for the posts controller| -|app/assets/stylesheets/scaffolds.css.scss |Cascading style sheet to make the scaffolded views look better| - -NOTE. While scaffolding will get you up and running quickly, the code it -generates is unlikely to be a perfect fit for your application. You'll most -probably want to customize the generated code. Many experienced Rails developers -avoid scaffolding entirely, preferring to write all or most of their source code -from scratch. Rails, however, makes it really simple to customize templates for -generated models, controllers, views and other source files. You'll find more -information in the "Creating and Customizing Rails Generators & -Templates":generators.html guide. +h4. Creating Posts + +The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at +/posts/new+. If you attempt to navigate to that now -- by visiting "http://localhost:3000/posts/new":http://localhost:3000/posts/new -- Rails will give you a routing error: + + +!images/getting_started/routing_error_no_route_matches.png(A routing error, no route matches /posts/new)! + +This is because there is nowhere inside the routes for the application -- defined inside +config/routes.rb+ -- that defines this route. By default, Rails has no routes configured at all, and so you must define your routes as you need them. + + To do this, you're going to need to create a route inside +config/routes.rb+ file, on a new line between the +do+ and the +end+ for the +draw+ method: + + + get "posts/new" + + +This route is a super-simple route: it defines a new route that only responds to +GET+ requests, and that the route is at +posts/new+. But how does it know where to go without the use of the +:to+ option? Well, Rails uses a sensible default here: Rails will assume that we want this route to go to the new action inside the posts controller. + +With the route defined, requests can now be made to +/posts/new+ in the application. Navigate to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and you'll see another routing error: + +!images/getting_started/routing_error_no_controller.png(Another routing error, uninitialized constant PostsController) + +This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. h4. Running a Migration From 605f4267bc4a98783430eb28a56e17c3f09e5871 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 18:49:52 -0700 Subject: [PATCH 15/28] [getting started] remove indentation for get 'posts/new' --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index c0af3cdc08c17..b1615cecb8ad5 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -233,7 +233,7 @@ This is because there is nowhere inside the routes for the application -- define To do this, you're going to need to create a route inside +config/routes.rb+ file, on a new line between the +do+ and the +end+ for the +draw+ method: - get "posts/new" +get "posts/new" This route is a super-simple route: it defines a new route that only responds to +GET+ requests, and that the route is at +posts/new+. But how does it know where to go without the use of the +:to+ option? Well, Rails uses a sensible default here: Rails will assume that we want this route to go to the new action inside the posts controller. From 75aa3dccb301f7e4505596480eceb27b7d91bfdb Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 18:51:42 -0700 Subject: [PATCH 16/28] [getting started] Improve explanation around initial controller generator usage --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index b1615cecb8ad5..9a08a6b7f4312 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -168,7 +168,7 @@ A controller's purpose is to receive specific requests for the application. What A view's purpose is to display this information in a human readable format. An important distinction to make is that it is the _controller_, not the view, where information is collected. The view should just display that information. By default, view templates are written in a language called ERB (Embedded Ruby) which is converted by the request cycle in Rails before being sent to the user. -To create a new controller, run this command: +To create a new controller, you will need to run the "controller" generator and tell it you want a controller called "welcome" with an action called "index", just like this: $ rails generate controller welcome index From 507dba67c425f185c49bbb751707e81cfd3e0b9c Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 19:30:57 -0700 Subject: [PATCH 17/28] [getting started] correct typo in welcome/index.html.erb template path --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 9a08a6b7f4312..ece017ae03d16 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -176,7 +176,7 @@ $ rails generate controller welcome index Rails will create several files for you. Most important of these are of course the controller, located at +app/controllers/welcome_controller.rb+ and the view, located at +app/views/welcome/index.html.erb+. -Open the +app/views/welcome/index/html.erb+ file in your text editor and edit it to contain a single line of code: +Open the +app/views/welcome/index.html.erb+ file in your text editor and edit it to contain a single line of code:

Hello, Rails!

From 7ea1755998c83595161bb2d39bf7a553080be8f5 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 19:31:38 -0700 Subject: [PATCH 18/28] [getting started] rename initial 'meat' section of guide to 'Laying down the ground work' This section involves a medium amount of setup and explanation (for the end user's own good!). It's not really about creating a post... that's for the *next* section --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index ece017ae03d16..35608333eb130 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -221,7 +221,7 @@ In the next section, you will add the ability to create new posts in your applic It will look a little basic for now, but that's ok. We'll look at improving the styling for it afterwards. -h4. Creating Posts +h4. Laying down the ground work The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at +/posts/new+. If you attempt to navigate to that now -- by visiting "http://localhost:3000/posts/new":http://localhost:3000/posts/new -- Rails will give you a routing error: From 9c3d0029e571cfdbd3b71c82836fc82350d5de09 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 19:32:13 -0700 Subject: [PATCH 19/28] [getting started] Round out 'Laying down the ground work' section --- .../guides/source/getting_started.textile | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 35608333eb130..b4bf2db3dd894 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -242,7 +242,68 @@ With the route defined, requests can now be made to +/posts/new+ in the applicat !images/getting_started/routing_error_no_controller.png(Another routing error, uninitialized constant PostsController) -This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. +This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. The solution to this particular problem is simple: you need to create a controller called +PostsController+. You can do this by running this command: + + +$ rails g controller posts + + +If you open up the newly generated +app/controllers/posts_controller.rb+ you'll see a fairly empty controller: + + +class PostsController < ApplicationController +end + + +A controller is simply a class that is defined to inherit from +ApplicationController+. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the posts within our system. + +If you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new now, you'll get a new error: + +!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!) + +This error indicates that Rails cannot find the +new+ action inside the +PostsController+ that you just generated. This is because when controllers are generated in Rails they are empty by default, unless you tell it you wanted actions during the generation process. + +To manually define an action inside a controller, all you need to do is to define a new method inside the controller. Open +app/controllers/posts_controller.rb+ and inside the +PostsController+ class, define a +new+ method like this: + + +def new +end + + +With the +new+ method defined in +PostsController+, if you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll see another error: + +!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new) + +We're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out. + +In the above image, the bottom line has been truncated. Let's see what the full thing looks like: + + +Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/path/to/blog/app/views" + + +That's quite a lot of text! Let's quickly go through and understand what each part of it does. + +The first part identifies what template is missing. In this case, it's the +posts/new+ template. Rails will first look for this template. If it can't find it, then it will attempt to load a template called +application/new+. It looks for one here because the +PostsController+ inherits from +ApplicationController+. + +The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template we want to retreive. By default, this is the English -- or "en" -- template. The next key, +:formats+ shows what formats of template we're after. The default is +:html+, and so we're looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates. + +The final part of this message tells us where Rails has looked for the templates. Templates within a basic Rails application like this are kept in a single location, but in more complex applications it could be many different paths. + +The simplest template that would work in this case would be one located at +app/views/posts/new.html.erb+. The extension of this file name is key: the first extension is the _format_ of the template, and the second extension is the _handler_ that will be used. Rails is attempting to find a template called +posts/new+ within +app/views+ for the application. The format for this template can only be +html+ and the handler must be one of +erb+, +builder+ or +coffee+. Because you want to create a new HTML form, you will be using the +ERB+ language. Therefore the file should be called +posts/new.html.erb+ and be located inside the +app/views+ directory of the application. + +Go ahead now and create a new file at +app/views/posts/new.html.erb+ and write this content in it: + + +

New Post

+
+ +When you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll now see that the page has a title. The route, controller, action and view are now working harmoniously! It's time to create the form for a new post. + +h4. The first form + + + h4. Running a Migration From b5e2518ddb9853f9a943a85ce5e194f9a21eea17 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 14 Mar 2012 23:47:39 -0700 Subject: [PATCH 20/28] [getting started] [ci skip] Fix incongruant use of 'we' and 'you'. Expand upon creating posts section --- .../guides/source/getting_started.textile | 86 ++++++++++++++++++- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index b4bf2db3dd894..8196a67d35f1d 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -188,7 +188,7 @@ Now that we have made the controller and view, we need to tell Rails when we wan To fix this, delete the +index.html+ file located inside the +public+ directory of the application. -We need to do this because Rails will serve any static file in the +public+ directory that matches a route in preference to any dynamic content we generate from the controllers. +You need to do this because Rails will serve any static file in the +public+ directory that matches a route in preference to any dynamic content you generate from the controllers. Next, you have to tell Rails where your actual home page is located. @@ -236,7 +236,7 @@ This is because there is nowhere inside the routes for the application -- define get "posts/new"
-This route is a super-simple route: it defines a new route that only responds to +GET+ requests, and that the route is at +posts/new+. But how does it know where to go without the use of the +:to+ option? Well, Rails uses a sensible default here: Rails will assume that we want this route to go to the new action inside the posts controller. +This route is a super-simple route: it defines a new route that only responds to +GET+ requests, and that the route is at +posts/new+. But how does it know where to go without the use of the +:to+ option? Well, Rails uses a sensible default here: Rails will assume that you want this route to go to the new action inside the posts controller. With the route defined, requests can now be made to +/posts/new+ in the application. Navigate to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and you'll see another routing error: @@ -274,7 +274,7 @@ With the +new+ method defined in +PostsController+, if you refresh "http://local !images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new) -We're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out. +You're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out. In the above image, the bottom line has been truncated. Let's see what the full thing looks like: @@ -286,7 +286,7 @@ That's quite a lot of text! Let's quickly go through and understand what each pa The first part identifies what template is missing. In this case, it's the +posts/new+ template. Rails will first look for this template. If it can't find it, then it will attempt to load a template called +application/new+. It looks for one here because the +PostsController+ inherits from +ApplicationController+. -The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template we want to retreive. By default, this is the English -- or "en" -- template. The next key, +:formats+ shows what formats of template we're after. The default is +:html+, and so we're looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates. +The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English -- or "en" -- template. The next key, +:formats+ shows what formats of template Rails is after. The default is +:html+, and so Rails is looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates. The final part of this message tells us where Rails has looked for the templates. Templates within a basic Rails application like this are kept in a single location, but in more complex applications it could be many different paths. @@ -302,6 +302,84 @@ When you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/n h4. The first form +To create a form within this template, you will use a _form builder_. The primary form builder for Rails is provided by a helper method called +form_for+. To use this method, write this code into +app/views/posts/new.html.erb+: + + +<%= form_for :post do |f| %> +

+ <%= f.label :title %>
+ <%= f.text_field :title %> +

+ +

+ <%= f.label :text %>
+ <%= f.text_area :text %> +

+ +

+ <%= f.submit %> +

+<% end %> +
+ +If you refresh the page now, you'll see the exact same form as in the example. Building forms in Rails is really just that easy! + +When you call +form_for+, you pass it an identifying object for this form. In this case, it's the symbol +:post+. This tells the +form_for+ helper what this form is for. Inside the block for this method, the FormBuilder object -- represented by +f+ -- is used to build two labels and two text fields, one each for the title and text of a post. Finally, a call to +submit+ on the +f+ object will create a submit button for the form. + +There's one problem with this form though. If you inspect the HTML that is generated, by viewing the source of the page, you will see that the +action+ attribute for the form is pointing at +/posts/new+. This is a problem because this route goes to the very page that you're on right at the moment, and that route should only be used to display the form for a new post. + +So the form needs to use a different URL in order to go somewhere else. This can be done quite simply with the +:url+ option of +form_for+. Typically in Rails, the action that is used for new form submissions like this is called "create", and so the form should be pointed to this action. + +Edit the +form_for+ line inside +app/views/posts/new.html.erb+ to look like this: + + +<%= form_for :post, :url => { :action => :create } do |f| %> + + +In this example, a +Hash+ object is passed to the +:url+ option. What Rails will do with this is that it will point the form to the +create+ action of the current controller, the +PostsController+, and will send a +POST+ request to that route. For this to work, you will need to add a route to +config/routes.rb+, right underneath the one for "posts/new": + + +post "posts/create" + + +By using the +post+ method rather than the +get+ method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web. + +With the form and the route for it defined now, you will be able to fill in the form and then click the submit button to begin the process of creating a new post, so go ahead and do that. When you submit the form, you should see a familiar error: + +!images/getting_started/unknown_action_create_for_posts(Unknown action create for PostsController)! + +You will now need to create the +create+ action within the +PostsController+ for this to work. + +h4. Creating posts + +To make the "Unknown action" go away, you can define a +create+ action within the +PostsController+ class in +app/controllers/posts_controller.rb+, underneath the +new+ action: + + +class PostsController < ApplicationController + def new + end + + def create + end + +end + + +If you re-submit the form now, you'll see another familiar error: a template is missing. That's ok, we can ignore that for now. What the +create+ action should be doing is saving our new post to a database. + +When a form is submitted, the fields of the form are sent to Rails as _parameters_. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the +create+ action to this: + + +def create + render :text => params.inspect +end + + +The +render+ method here is taking a very simple hash with the key of +text+ and the value of +params.inspect+. The +params+ method here is the object which represents the parameters (or fields) coming in from the form. If you re-submit the form one more time you'll now no longer get the missing template error. Instead, you'll see something that looks like the following: + + +{"title"=>"First post!", "text"=>"This is my first post."} + From 2784e6433bd8590a51e3dacc5fcc01e9dcdcea26 Mon Sep 17 00:00:00 2001 From: adman65 Date: Thu, 15 Mar 2012 10:08:16 +0100 Subject: [PATCH 21/28] [ci skip] Add examples to instrumentation guide --- .../active_support_instrumentation.textile | 364 ++++++++++++++++-- 1 file changed, 322 insertions(+), 42 deletions(-) diff --git a/railties/guides/source/active_support_instrumentation.textile b/railties/guides/source/active_support_instrumentation.textile index 8e2866dfc351f..18500949e0f5e 100644 --- a/railties/guides/source/active_support_instrumentation.textile +++ b/railties/guides/source/active_support_instrumentation.textile @@ -23,72 +23,352 @@ h3. Rails framework hooks Within the Ruby on Rails framework, there are a number of hooks provided for common events. These are detailed below. -h4. Action Mailer +h3. ActionController -h5. receive.action_mailer +h4. write_fragment.action_controller -This hook is called when the +receive+ method of an +ActionMailer::Base+ class is called: +|_.Key |_.Value| +|+:key+ |The complete key| - class Mailer < ActionMailer::Base - def receive(mail) +{ + :key => 'posts/1-dasboard-view' +} + + +h4. read_fragment.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + + +{ + :key => 'posts/1-dasboard-view' +} + + +h4. expire_fragment.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + + +{ + :key => 'posts/1-dasboard-view' +} + + +h4. exist_fragment?.action_controller + +|_.Key |_.Value| +|+:key+ |The complete key| + + +{ + :key => 'posts/1-dasboard-view' +} + + +h4. write_page.action_controller + +|_.Key |_.Value| +|+:path+ |The complete path| + + +{ + :path => '/users/1' +} + + +h4. expire_page.action_controller + +|_.Key |_.Value| +|+:path+ |The complete path| + + +{ + :path => '/users/1' +} + - end - end +h4. start_processing.action_controller + +|_.Key |_.Value | +|+:controller+ |The controller name| +|+:action+ |The action| +|+:params+ |Hash of request parameters without any filtered parameter| +|+:format+ |html/js/json/xml etc| +|+:method+ |HTTP request verb| +|+:path+ |Request path| + + +{ + :controller => "PostsController", + :action => "new", + :params => { "action" => "new", "controller" => "posts" }, + :format => :html, + :method => "GET", + :path => "/posts/new" +} + + +h4. process_action.action_controller + +|_.Key |_.Value | +|+:controller+ |The controller name| +|+:action+ |The action| +|+:params+ |Hash of request parameters without any filtered parameter| +|+:format+ |html/js/json/xml etc| +|+:method+ |HTTP request verb| +|+:path+ |Request path| +|+:view_runtime+ |Amount spent in view in ms| + + +{ + :controller => "PostsController", + :action => "index", + :params => {"action" => "index", "controller" => "posts"}, + :format => :html, + :method => "GET", + :path => "/posts", + :status => 200, + :view_runtime => 46.848, + :db_runtime => 0.157 +} + + +h4. send_file.action_controller + +|_.Key |_.Value | +|+:path+ |Complete path to the file| + +INFO. Additional keys may be added by the caller. + +h4. send_data.action_controller + ++ActionController+ does not had any specific information to the payload. All options are passed through to the payload. + +h4. redirect_to.action_controller + +|_.Key |_.Value | +|+:status+ |HTTP response code| +|+:location+ |URL to redirect to| + + +{ + :status => 302, + :location => "http://localhost:3000/posts/new" +} -The payload for this event has the following parameters related to the incoming email: +h4. halted_callback.action_controller -|_.Key |_.Value| -|mailer |Name of the mailer class| -|message_id |ID of the message, generated by the Mail gem| -|subject |Subject of the mail| -|to |To address(es) of the mail| -|from |From address of the mail| -|bcc |BCC addresses of the mail| -|cc |CC addresses of the mail| -|date |Date of the mail| -|mail |The encoded form of the mail| +|_.Key |_.Value | +|+:filter+ |Filter that halted the action| -h5. deliver.action_mailer + +{ + :filter => ":halting_filter" +} + -This hook is called when the +deliver+ method is called on a +Mail::Message+ object. This is due to a hook inserted by Action Mailer, rather than a specific feature of the Mail gem itself. +h3. ActionView -The payload for this event has the following parameters related to the outgoing email: +h4. render_template.action_view -|_.Key |_.Value| -|mailer |Name of the mailer class| -|message_id |ID of the message, generated by the Mail gem| -|subject |Subject of the mail| -|to |To address(es) of the mail| -|from |From address of the mail| -|bcc |BCC addresses of the mail| -|cc |CC addresses of the mail| -|date |Date of the mail| -|mail |The encoded form of the mail| +|_.Key |_.Value | +|+:identifier+ |Full path to template| +|+:layout+ |Applicable layout| + +{ + :identifier => "/Users/adam/projects/notifications/app/views/posts/index.html.erb", + :layout => "layouts/application" +} + + +h4. render_partial.action_view -h4. Action Controller +|_.Key |_.Value | +|+:identifier+ |Full path to template| -h5. write_fragment.action_controller + +{ + :identifier => "/Users/adam/projects/notifications/app/views/posts/_form.html.erb", +} + -h5. read_fragment.action_controller +h3. ActiveRecord -h5. exist_fragment?.action_controller +h4. sql.active_record -h5. expire_fragment.action_controller +|_.Key |_.Value | +|+:sql+ |SQL statement| +|+:name+ |Name of the operation| +|+:object_id+ |+self.object_id+| -h5. write_page.action_controller +INFO. The adapters will add their own data as well. -h5. expire_page.action_controller + +{ + :sql => "SELECT \"posts\".* FROM \"posts\" ", + :name => "Post Load", + :connection_id => 70307250813140, + :binds => [] +} + + +h4. identity.active_record + +|_.Key |_.Value | +|+:line+ |Primary Key of object in the identity map| +|+:name+ |Record's class| +|+:connection_id+ |+self.object_id+| + +h3. ActionMailer + +h4. receive.action_mailer + +|_.Key |_.Value| +|+:mailer+ |Name of the mailer class| +|+:message_id+ |ID of the message, generated by the Mail gem| +|+:subject+ |Subject of the mail| +|+:to+ |To address(es) of the mail| +|+:from+ |From address of the mail| +|+:bcc+ |BCC addresses of the mail| +|+:cc+ |CC addresses of the mail| +|+:date+ |Date of the mail| +|+:mail+ |The encoded form of the mail| + + +{ + :mailer => "Notification", + :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", + :subject => "Rails Guides", + :to => ["users@rails.com", "ddh@rails.com"], + :from => ["me@rails.com"], + :date => Sat, 10 Mar 2012 14:18:09 +0100, + :mail=> "..." # ommitted for beverity +} + + +h4. deliver.action_mailer + +|_.Key |_.Value| +|+:mailer+ |Name of the mailer class| +|+:message_id+ |ID of the message, generated by the Mail gem| +|+:subject+ |Subject of the mail| +|+:to+ |To address(es) of the mail| +|+:from+ |From address of the mail| +|+:bcc+ |BCC addresses of the mail| +|+:cc+ |CC addresses of the mail| +|+:date+ |Date of the mail| +|+:mail+ |The encoded form of the mail| + + +{ + :mailer => "Notification", + :message_id => "4f5b5491f1774_181b23fc3d4434d38138e5@mba.local.mail", + :subject => "Rails Guides", + :to => ["users@rails.com", "ddh@rails.com"], + :from => ["me@rails.com"], + :date => Sat, 10 Mar 2012 14:18:09 +0100, + :mail=> "..." # ommitted for beverity +} + + +h3. ActiveResource + +h4. request.active_resource + +|_.Key |_.Value| +|+:method+ |HTTP method| +|+:request_uri+ |Complete URI| +|+:result+ |HTTP response object| + +h3. ActiveSupport + +h4. cache_read.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| +|+:hit+ |If this read is a hit| +|+:super_operation+ |:fetch is added when a read is used with +#fetch+| + +h4. cache_generate.active_support + +This event is only used when +#fetch+ is called with a block. + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +INFO. Options passed to fetch will be merged with the payload when writing to the store + + +{ + :key => 'name-of-complicated-computation' +} + + + +h4. cache_fetch_hit.active_support + +This event is only used when +#fetch+ is called with a block. + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +INFO. Options passed to fetch will be merged with the payload. + + +{ + :key => 'name-of-complicated-computation' +} + + +h4. cache_write.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| + +INFO. Cache stores my add their own keys + + +{ + :key => 'name-of-complicated-computation' +} + + +h4. cache_delete.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| + + +{ + :key => 'name-of-complicated-computation' +} + + +h4. cache_exist?.active_support + +|_.Key |_.Value| +|+:key+ |Key used in the store| + + +{ + :key => 'name-of-complicated-computation' +} + -h4. Action View -h4. Active Record +h3. Rails -h4. Active Resource +h4. deprecation.rails -h4. Active Support +|_.Key |_.Value| +|+:message+ |The deprecation warning| +|+:callstack+ |Where the deprecation came from| h3. Subscribing to an event From 18e1b5b8c5d7c5fd4a73a9f896f59e1ab03781bf Mon Sep 17 00:00:00 2001 From: adman65 Date: Thu, 15 Mar 2012 10:27:04 +0100 Subject: [PATCH 22/28] [ci skip] Add examples of subscribing & creating ActiveSupport::Notifications --- .../active_support_instrumentation.textile | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/active_support_instrumentation.textile b/railties/guides/source/active_support_instrumentation.textile index 18500949e0f5e..dbf701acee89e 100644 --- a/railties/guides/source/active_support_instrumentation.textile +++ b/railties/guides/source/active_support_instrumentation.textile @@ -361,7 +361,6 @@ h4. cache_exist?.active_support } - h3. Rails h4. deprecation.rails @@ -372,5 +371,78 @@ h4. deprecation.rails h3. Subscribing to an event +Subscribing to an event is easy. Use +ActiveSupport::Notifications.subscribe+ with a block to +listen to any notification. + +The block receives the following arguments: + +# The name of the event +# Time when is started +# Time when it finished +# An unique ID for this event +# The payload (described in previous sections) + + +ActiveSupport::Notifications.subscribe "process_action.action_controller do |name, started, finished, unique_id, data| + # your own custom stuff + Rails.logger.info "#{name} Received!" +end + + +Defining all those block arguments each time can be tedious. You can easily create an +ActiveSupport::Notifications::Event+ +from block args like this: + + +ActiveSupport::Notifications.subscribe "process_action.action_controller do |*args| + event = ActiveSupport::Notification::Event.new args + + event.name # => "process_action.action_controller" + event.duration # => 10 (in milliseconds) + event.payload # => { :extra => :information } + + Rails.logger.info "#{event} Received!" +end + + +Most times you only care about the data itself. Here is a shortuct to just get the data. + + +ActiveSupport::Notifications.subscribe "process_action.action_controller do |*args| + data = args.extract_options! + data # { :extra => :information } + + +You may also subscribe to events matching a regular expresssion. This enables you to subscribe to +multiple events at once. Here's you could subscribe to everything from +ActionController+. + + +ActiveSupport::Notifications.subscribe /action_controller/ do |*args| + # inspect all ActionController events +end + + h3. Creating custom events +Adding your own events is easy as well. +ActiveSupport::Notifications+ will take care of +all the heavy lifting for you. Simply call +instrument+ with a +name+, +payload+ and a block. +The notification will be sent after the block returns. +ActiveSupport+ will generate the start and end times +as well as the unique ID. All data passed into the +insturment+ call will make it into the payload. + +Here's an example: + + +ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do + # do your custom stuff here +end + + +Now you can listen to this event with: + + +ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, finished, unique_id, data| + puts data.inspect # { :this => :data } +end + + +You should follow Rails conventions when defining your own events. The format is: +event.library+. +If you application is sending Tweets, you should create an event named +tweet.twitter+. From 55df1df937f096bbdae29f8af965f204f08943b8 Mon Sep 17 00:00:00 2001 From: Waseem Ahmad Date: Thu, 15 Mar 2012 16:07:53 +0530 Subject: [PATCH 23/28] Fix typo in AR Callbacks. --- activerecord/lib/active_record/callbacks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index a050fabf355fb..91d9b3f3d704e 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -3,7 +3,7 @@ module ActiveRecord # # Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic # before or after an alteration of the object state. This can be used to make sure that associated and - # dependent objects are deleted when +destroy+ is called (by overwriting +before_destroy+) or to massage attributes + # dependent objects are deleted when +destroy+ is called (by overwriting +before_destroy+) or to message attributes # before they're validated (by overwriting +before_validation+). As an example of the callbacks initiated, consider # the Base#save call for a new record: # From a1c77e7d947cf61ae01869075282c66816add419 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 15 Mar 2012 09:41:28 -0300 Subject: [PATCH 24/28] Add missing quotes on AS::Notifications examples --- .../active_support_instrumentation.textile | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/railties/guides/source/active_support_instrumentation.textile b/railties/guides/source/active_support_instrumentation.textile index dbf701acee89e..430549fba463a 100644 --- a/railties/guides/source/active_support_instrumentation.textile +++ b/railties/guides/source/active_support_instrumentation.textile @@ -156,7 +156,7 @@ h4. redirect_to.action_controller { - :status => 302, + :status => 302, :location => "http://localhost:3000/posts/new" } @@ -305,7 +305,7 @@ This event is only used when +#fetch+ is called with a block. INFO. Options passed to fetch will be merged with the payload when writing to the store -{ +{ :key => 'name-of-complicated-computation' } @@ -321,7 +321,7 @@ This event is only used when +#fetch+ is called with a block. INFO. Options passed to fetch will be merged with the payload. -{ +{ :key => 'name-of-complicated-computation' } @@ -334,7 +334,7 @@ h4. cache_write.active_support INFO. Cache stores my add their own keys -{ +{ :key => 'name-of-complicated-computation' } @@ -345,7 +345,7 @@ h4. cache_delete.active_support |+:key+ |Key used in the store| -{ +{ :key => 'name-of-complicated-computation' } @@ -356,7 +356,7 @@ h4. cache_exist?.active_support |+:key+ |Key used in the store| -{ +{ :key => 'name-of-complicated-computation' } @@ -371,7 +371,7 @@ h4. deprecation.rails h3. Subscribing to an event -Subscribing to an event is easy. Use +ActiveSupport::Notifications.subscribe+ with a block to +Subscribing to an event is easy. Use +ActiveSupport::Notifications.subscribe+ with a block to listen to any notification. The block receives the following arguments: @@ -383,7 +383,7 @@ The block receives the following arguments: # The payload (described in previous sections) -ActiveSupport::Notifications.subscribe "process_action.action_controller do |name, started, finished, unique_id, data| +ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data| # your own custom stuff Rails.logger.info "#{name} Received!" end @@ -393,7 +393,7 @@ Defining all those block arguments each time can be tedious. You can easily crea from block args like this: -ActiveSupport::Notifications.subscribe "process_action.action_controller do |*args| +ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| event = ActiveSupport::Notification::Event.new args event.name # => "process_action.action_controller" @@ -407,7 +407,7 @@ end Most times you only care about the data itself. Here is a shortuct to just get the data. -ActiveSupport::Notifications.subscribe "process_action.action_controller do |*args| +ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| data = args.extract_options! data # { :extra => :information } @@ -431,7 +431,7 @@ as well as the unique ID. All data passed into the +insturment+ call will make i Here's an example: -ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do +ActiveSupport::Notifications.instrument "my.custom.event", :this => :data do # do your custom stuff here end @@ -444,5 +444,5 @@ ActiveSupport::Notifications.subscribe "my.custom.event" do |name, started, fini end -You should follow Rails conventions when defining your own events. The format is: +event.library+. +You should follow Rails conventions when defining your own events. The format is: +event.library+. If you application is sending Tweets, you should create an event named +tweet.twitter+. From 9dba310b1e03fc292071b39bc0cb60618c765324 Mon Sep 17 00:00:00 2001 From: Mark Larimer Date: Fri, 16 Mar 2012 11:54:44 -0500 Subject: [PATCH 25/28] Revert "Fix typo in AR Callbacks." This reverts commit 55df1df937f096bbdae29f8af965f204f08943b8. --- activerecord/lib/active_record/callbacks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 91d9b3f3d704e..a050fabf355fb 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -3,7 +3,7 @@ module ActiveRecord # # Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic # before or after an alteration of the object state. This can be used to make sure that associated and - # dependent objects are deleted when +destroy+ is called (by overwriting +before_destroy+) or to message attributes + # dependent objects are deleted when +destroy+ is called (by overwriting +before_destroy+) or to massage attributes # before they're validated (by overwriting +before_validation+). As an example of the callbacks initiated, consider # the Base#save call for a new record: # From 2dd486ceb568b6bd18825f947e6e936203eaabd8 Mon Sep 17 00:00:00 2001 From: Mike Mulvaney Date: Fri, 16 Mar 2012 15:50:53 -0400 Subject: [PATCH 26/28] Changed unit tests so they refer to yaffle_test_field as symbol. acts_as_yaffle expects a symbol, so the string was causing the tests to always fail. --- railties/guides/source/plugins.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/railties/guides/source/plugins.textile b/railties/guides/source/plugins.textile index 07fd95c82584d..5d2f0917b7abd 100644 --- a/railties/guides/source/plugins.textile +++ b/railties/guides/source/plugins.textile @@ -176,11 +176,11 @@ require 'test_helper' class ActsAsYaffleTest < Test::Unit::TestCase def test_a_hickwalls_yaffle_text_field_should_be_last_squawk - assert_equal "last_squawk", Hickwall.yaffle_text_field + assert_equal :last_squawk, Hickwall.yaffle_text_field end def test_a_wickwalls_yaffle_text_field_should_be_last_tweet - assert_equal "last_tweet", Wickwall.yaffle_text_field + assert_equal :last_tweet, Wickwall.yaffle_text_field end end From f820098f9ef8fe6e1a5f188d01c3eb2586c5546e Mon Sep 17 00:00:00 2001 From: Mike Mulvaney Date: Fri, 16 Mar 2012 15:53:16 -0400 Subject: [PATCH 27/28] Moved squawk method into LocalInstanceMethods module, so it would not be added to every ActiveRecord::Base object. Changed acts_as_yaffle so it will include the LocalInstanceMethods module. Now only models that call acts_as_yaffle will get the squawk method, instead of pushing them into every ActiveRecord::Base. --- railties/guides/source/plugins.textile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/railties/guides/source/plugins.textile b/railties/guides/source/plugins.textile index 5d2f0917b7abd..97b4eca77974d 100644 --- a/railties/guides/source/plugins.textile +++ b/railties/guides/source/plugins.textile @@ -362,13 +362,16 @@ module Yaffle def acts_as_yaffle(options = {}) cattr_accessor :yaffle_text_field self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s + + include Yaffle::ActsAsYaffle::LocalInstanceMethods end end - def squawk(string) - write_attribute(self.class.yaffle_text_field, string.to_squawk) + module LocalInstanceMethods + def squawk(string) + write_attribute(self.class.yaffle_text_field, string.to_squawk) + end end - end end From bf94f1cab77fc4eb7fc806fd43120f9f23b451ad Mon Sep 17 00:00:00 2001 From: Egor Homakov Date: Sat, 17 Mar 2012 07:05:38 +0100 Subject: [PATCH 28/28] using pluck --- railties/guides/source/action_mailer_basics.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/action_mailer_basics.textile b/railties/guides/source/action_mailer_basics.textile index 2760e03be1375..c277f764e7e28 100644 --- a/railties/guides/source/action_mailer_basics.textile +++ b/railties/guides/source/action_mailer_basics.textile @@ -244,7 +244,7 @@ It is possible to send email to one or more recipients in one email (for e.g. in class AdminMailer < ActionMailer::Base - default :to => Proc.new { Admin.all.map(&:email) }, + default :to => Proc.new { Admin.pluck(:email) }, :from => "notification@example.com" def new_registration(user)