From fce160f00ef1e473dac2c6fc2f67f17a8a300364 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:08:51 -0500 Subject: [PATCH 01/21] Edit Getting Started guide introductory sections [ci-skip] --- guides/source/getting_started.md | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 4b79043add..e326e36039 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -30,8 +30,8 @@ for learning Ruby: * [Official Ruby Programming Language website](https://www.ruby-lang.org/en/documentation/) * [List of Free Programming Books](https://github.com/EbookFoundation/free-programming-books/blob/master/free-programming-books.md#ruby) -Be aware that some resources, while still excellent, cover versions of Ruby as old as -1.6, and commonly 1.8, and will not include some syntax that you will see in day-to-day +Be aware that some resources, while still excellent, cover older versions of +Ruby, and may not include some syntax that you will see in day-to-day development with Rails. What is Rails? @@ -63,6 +63,7 @@ The Rails philosophy includes two major guiding principles: Creating a New Rails Project ---------------------------- + The best way to read this guide is to follow it step by step. All steps are essential to run this example application and no additional code or steps are needed. @@ -71,9 +72,9 @@ By following along with this guide, you'll create a Rails project called `blog`, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed. -TIP: The examples below use `$` to represent your terminal prompt in a UNIX-like OS, +NOTE: The examples below use `$` to represent your terminal prompt in a UNIX-like OS, though it may have been customized to appear differently. If you are using Windows, -your prompt will look something like `c:\source_code>` +your prompt will look something like `C:\source_code>`. ### Installing Rails @@ -87,8 +88,8 @@ proper prerequisites installed. These include: #### Installing Ruby -Open up a command line prompt. On macOS open Terminal.app, on Windows choose -"Run" from your Start menu and type 'cmd.exe'. Any commands prefaced with a +Open up a command line prompt. On macOS open Terminal.app; on Windows choose +"Run" from your Start menu and type `cmd.exe`. Any commands prefaced with a dollar sign `$` should be run in the command line. Verify that you have a current version of Ruby installed: @@ -170,7 +171,7 @@ which will provide you with the foundation of a fresh 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: +rights to create files, and run: ```bash $ rails new blog @@ -182,10 +183,11 @@ install the gem dependencies that are already mentioned in `Gemfile` using NOTE: If you're using Windows Subsystem for Linux then there are currently some limitations on file system notifications that mean you should disable the `spring` -and `listen` gems which you can do by running `rails new blog --skip-spring --skip-listen`. +and `listen` gems which you can do by running `rails new blog --skip-spring --skip-listen` +instead. TIP: You can see all of the command line options that the Rails application -builder accepts by running `rails new -h`. +generator accepts by running `rails new --help`. After you create the blog application, switch to its folder: @@ -193,30 +195,30 @@ After you create the blog application, switch to its folder: $ cd blog ``` -The `blog` directory has a number of auto-generated files and folders that make +The `blog` directory will have a number of generated files and 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: +of the files and folders that Rails creates by default: | File/Folder | Purpose | | ----------- | ------- | |app/|Contains the controllers, models, views, helpers, mailers, channels, jobs, and assets for your application. You'll focus on this folder for the remainder of this guide.| -|bin/|Contains the rails script that starts your app and can contain other scripts you use to set up, update, deploy, or run your application.| -|config/|Configure your application's routes, database, and more. This is covered in more detail in [Configuring Rails Applications](configuring.html).| -|config.ru|Rack configuration for Rack based servers used to start the application. For more information about Rack, see the [Rack website](https://rack.github.io/).| +|bin/|Contains the `rails` script that starts your app and can contain other scripts you use to set up, update, deploy, or run your application.| +|config/|Contains configuration for your application's routes, database, and more. This is covered in more detail in [Configuring Rails Applications](configuring.html).| +|config.ru|Rack configuration for Rack-based servers used to start the application. For more information about Rack, see the [Rack website](https://rack.github.io/).| |db/|Contains your current database schema, as well as the database migrations.| |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](https://bundler.io).| |lib/|Extended modules for your application.| |log/|Application log files.| |package.json|This file allows you to specify what npm dependencies are needed for your Rails application. This file is used by Yarn. For more information about Yarn, see the [Yarn website](https://yarnpkg.com/lang/en/).| -|public/|The only folder seen by the world as-is. Contains static files and compiled assets.| +|public/|Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.| |Rakefile|This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing `Rakefile`, you should add your own tasks by adding files to the `lib/tasks` directory of your application.| |README.md|This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.| |storage/|Active Storage files for Disk Service. This is covered in [Active Storage Overview](active_storage_overview.html).| |test/|Unit tests, fixtures, and other test apparatus. These are covered in [Testing Rails Applications](testing.html).| |tmp/|Temporary files (like cache and pid files).| |vendor/|A place for all third-party code. In a typical Rails application this includes vendored gems.| -|.gitignore|This file tells git which files (or patterns) it should ignore. See [GitHub - Ignoring files](https://help.github.com/articles/ignoring-files) for more info about ignoring files. +|.gitignore|This file tells git which files (or patterns) it should ignore. See [GitHub - Ignoring files](https://help.github.com/articles/ignoring-files) for more info about ignoring files.| |.ruby-version|This file contains the default Ruby version.| Hello, Rails! @@ -229,7 +231,7 @@ get your Rails application server 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 the -following in the `blog` directory: +following command in the `blog` directory: ```bash $ bin/rails server @@ -246,16 +248,14 @@ Usually macOS and Windows come with a JavaScript runtime installed. default to the `Gemfile` in apps generated under JRuby. You can investigate all the supported runtimes at [ExecJS](https://github.com/rails/execjs#readme). -This will fire up Puma, a web server distributed with Rails by default. To see +This will start up Puma, a web server distributed with Rails by default. To see your application in action, open a browser window and navigate to . You should see the Rails default information page: ![Yay! You're on Rails! screenshot](images/getting_started/rails_welcome.png) -TIP: To stop the web server, hit Ctrl+C in the terminal window where it's -running. To verify the server has stopped you should see your command prompt -cursor again. For most UNIX-like systems including macOS this will be a -dollar sign `$`. In the development environment, Rails does not generally +When you want to stop the web server, hit Ctrl+C in the terminal window where +it's running. In the development environment, Rails does not generally require you to restart the server; changes you make in files will be automatically picked up by the server. From 3e182a2d4d37f8b521af39246b1057a64fe9b4ff Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:19:02 -0500 Subject: [PATCH 02/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Say=20"Hello?= =?UTF-8?q?",=20Rails=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves #40326. Co-Authored-By: Ian Robinson --- guides/source/getting_started.md | 82 +++++++++++--------------------- 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index e326e36039..2b1d9335ad 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -265,24 +265,19 @@ enough to serve a page. ### Say "Hello", Rails -To get Rails saying "Hello", you need to create at minimum a _route_, a _controller_ and a _view_. +To get Rails saying "Hello", you need to create at minimum a *route*, a +*controller* with an *action*, and a *view*. A route maps a request to a +controller action. A controller action performs the necessary work to handle the +request, and prepares any data for the view. A view displays data in a desired +format. -A controller's purpose is to receive specific requests for the application. -_Routing_ decides which controller receives which requests. Often, there is 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. +In terms of implementation: Routes are rules written in a Ruby [DSL +(Domain-Specific Language)](https://en.wikipedia.org/wiki/Domain-specific_language). +Controllers are Ruby classes, and their public methods are actions. And views +are templates, usually written in a mixture of HTML and Ruby. -A view's purpose is to display this information in a human readable format. An -important distinction to make is that the _controller_, not the view, -is where information is collected. The view should just display that information. -By default, view templates are written in a language called eRuby (Embedded -Ruby) which is processed by the request cycle in Rails before being sent to the -user. - -When we make a request to our Rails application, we do so by making a request -to a particular _route_. To start off, let's create a route in -`config/routes.rb`: +Let's start by adding a route to our routes file, `config/routes.rb`, at the +top of the `Rails.application.routes.draw` block: ```ruby Rails.application.routes.draw do @@ -292,33 +287,21 @@ Rails.application.routes.draw do end ``` -This is your application's _routing file_ which holds entries in a special [DSL -(domain-specific -language)](https://en.wikipedia.org/wiki/Domain-specific_language) that tells -Rails how to connect incoming requests to controllers and actions. +The route above declares that `GET /articles` requests are mapped to the `index` +action of `ArticlesController`. -The line that we have just added says that we are going to match a `GET -/articles` request to `articles#index`. This string passed as the `to` option -represents the _controller_ and _action_ that will be responsible for handling -this request. - -Controllers are classes that group together common methods for handling a -particular _resource_. The methods inside controllers are given the name -"actions", as they _act upon_ requests as they come in. - -To create a new controller, you will need to run the "controller" generator and -tell it you want a controller called "articles" with an action called "index", -just like this: +To create `ArticlesController` and its `index` action, we'll run the controller +generator (with the `--skip-routes` option because we already have an +appropriate route): ```bash -$ bin/rails generate controller articles index +$ bin/rails generate controller Articles index --skip-routes ``` -Rails will create several files and a route for you. +Rails will create several files for you: ``` create app/controllers/articles_controller.rb - route get 'articles/index' invoke erb create app/views/articles create app/views/articles/index.html.erb @@ -332,10 +315,8 @@ invoke scss create app/assets/stylesheets/articles.scss ``` -Most important of these is the controller, located at -`app/controllers/articles_controller.rb`. - -Let's look at that controller now: +The most important of these is the controller file, +`app/controllers/articles_controller.rb`. Let's take a look at it: ```ruby class ArticlesController < ApplicationController @@ -344,26 +325,21 @@ class ArticlesController < ApplicationController end ``` -This controller defines a single action, or "method" in common Ruby terms, -called `index`. This action is where we would define any logic that we would -want to happen when a request comes in to this action. Right at this moment, we -don't want this action to do anything, and so we'll keep it blank for now. +The `index` action is empty. When an action does not explicitly render a view +(or otherwise trigger an HTTP response), Rails will automatically render a view +that matches the name of the controller and action. Convention Over +Configuration! Views are located in the `app/views` directory. So the `index` +action will render `app/views/articles/index.html.erb` by default. -When an action is left blank like this, Rails will default to rendering a view -that matches the name of the controller and the name of the action. Views in a -Rails application live in `app/views`, and so the default view for this action -is going to be `app/views/articles/index.html.erb`. - -Open the `app/views/articles/index.html.erb` file in your text editor. Delete all -of the existing code in the file, and replace it with the following single line -of code: +Let's open `app/views/articles/index.html.erb`, and replace its contents with: ```html

Hello, Rails!

``` -If we go back to our browser and make a request to -, we'll see our text appear on the page. +If you previously stopped the web server to run the controller generator, +restart it with `bin/rails server`. Now visit , +and see our text displayed! ### Setting the Application Home Page From 428a95170c895038114d937b5330881966064f73 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:24:21 -0500 Subject: [PATCH 03/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Setting=20th?= =?UTF-8?q?e=20Application=20Home=20Page=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Resolves #40039. Resolves #40326. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas Co-Authored-By: Ian Robinson --- guides/source/getting_started.md | 49 +++++++++----------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 2b1d9335ad..e97794af95 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -343,51 +343,28 @@ and see our text displayed! ### 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, . At the moment, -"Yay! You're on Rails!" is occupying that spot. +At the moment, still displays "Yay! You're on Rails!". +Let's display our "Hello, Rails!" text at as well. To do +so, we will add a route that maps the *root path* of our application to the +appropriate controller and action. -Next, you have to tell Rails where your actual home page is located. - -Open the file `config/routes.rb` in your editor. +Let's open `config/routes.rb`, and add the following `root` route to the top of +the `Rails.application.routes.draw` block: ```ruby Rails.application.routes.draw do - get 'welcome/index' + root "articles#index" - # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + get "/articles", to: "articles#index" end ``` -This is your application's _routing file_ which holds entries in a special -[DSL (domain-specific language)](https://en.wikipedia.org/wiki/Domain-specific_language) -that tells Rails how to connect incoming requests to -controllers and actions. -Edit this file by adding the line of code `root 'welcome#index'`. -It should look something like the following: +Now we can see our "Hello, Rails!" text when we visit , +confirming that the `root` route is also mapped to the `index` action of +`ArticlesController`. -```ruby -Rails.application.routes.draw do - get 'welcome/index' - - root 'welcome#index' -end -``` - -`root 'welcome#index'` tells Rails to map requests to the root of the -application to the welcome controller's index action and `get 'welcome/index'` -tells Rails to map requests to to the -welcome controller's index action. This was created earlier when you ran the -controller generator (`bin/rails generate controller Welcome index`). - -Launch the web server again if you stopped it to generate the controller (`bin/rails -server`) and navigate to in your browser. You'll see the -"Hello, Rails!" message you put into `app/views/welcome/index.html.erb`, -indicating that this new route is indeed going to `WelcomeController`'s `index` -action and is rendering the view correctly. - -TIP: For more information about routing, refer to [Rails Routing from the Outside In](routing.html). +TIP: To learn more about routing, see [Rails Routing from the Outside In]( +routing.html). Getting Up and Running ---------------------- From 5f085fac7420aea39c424bdd04d5d357e6aabb2d Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:32:44 -0500 Subject: [PATCH 04/21] =?UTF-8?q?Add=20GS=20guide=20=C2=A7=20MVC=20and=20Y?= =?UTF-8?q?ou=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Resolves #40040. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas --- guides/source/getting_started.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index e97794af95..a8eeaeef77 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -366,6 +366,18 @@ confirming that the `root` route is also mapped to the `index` action of TIP: To learn more about routing, see [Rails Routing from the Outside In]( routing.html). +MVC and You +----------- + +So far, we've discussed routes, controllers, actions, and views. All of these +are typical pieces of a web application that follows the [MVC (Model-View-Controller)]( +https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) pattern. +MVC is a design pattern that divides the responsibilities of an application to +make it easier to reason about. Rails follows this design pattern by convention. + +Since we have a controller and a view to work with, let's generate the next +piece: a model. + Getting Up and Running ---------------------- From f492209067d9783ab12bbd2dfb7b7ab55dd62dc0 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:34:09 -0500 Subject: [PATCH 05/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Generating?= =?UTF-8?q?=20a=20Model=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Resolves #40040. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas --- guides/source/getting_started.md | 57 ++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index a8eeaeef77..83ac14dc00 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -378,6 +378,38 @@ make it easier to reason about. Rails follows this design pattern by convention. Since we have a controller and a view to work with, let's generate the next piece: a model. +### Generating a Model + +A *model* is a Ruby class that is used to represent data. Additionally, models +can interact with the application's database through a feature of Rails called +*Active Record*. + +To define a model, we will use the model generator: + +```bash +$ bin/rails generate model Article title:string body:text +``` + +NOTE: Model names are **singular**, because an instantiated model represents a +single data record. To help remember this convention, think of how you would +call the model's constructor: we want to write `Article.new(...)`, **not** +`Articles.new(...)`. + +This will create several files: + +``` +invoke active_record +create db/migrate/_create_articles.rb +create app/models/article.rb +invoke test_unit +create test/models/article_test.rb +create test/fixtures/articles.yml +``` + +The two files we'll focus on are the migration file +(`db/migrate/_create_articles.rb`) and the model file +(`app/models/article.rb`). + Getting Up and Running ---------------------- @@ -686,31 +718,6 @@ This action is now displaying the parameters for the article that are coming in from the form. However, this isn't really all that helpful. Yes, you can see the parameters but nothing in particular is being done with them. -### Creating the Article Model - -Models in Rails use a singular name, and their corresponding database tables -use a plural name. Rails provides a generator for creating models, which most -Rails developers tend to use when creating new models. To create the new model, -run this command in your terminal: - -```bash -$ bin/rails generate model Article title:string text:text -``` - -With that command we told Rails that we want an `Article` model, together -with a _title_ attribute of type string, and a _text_ attribute -of type text. Those attributes are automatically added to the `articles` -table in the database and mapped to the `Article` model. - -Rails responded by creating a bunch of files. For now, we're only interested -in `app/models/article.rb` and `db/migrate/20140120191729_create_articles.rb` -(your name could be a bit different). The latter is responsible for creating -the database structure, which is what we'll look at next. - -TIP: Active Record is smart enough to automatically map column names to model -attributes, which means you don't have to declare attributes inside Rails -models, as that will be done automatically by Active Record. - ### Running a Migration As we've just seen, `bin/rails generate model` created a _database migration_ file From 5ecb7881de88d75002b52ddc336d13a8397ebb45 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:36:19 -0500 Subject: [PATCH 06/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Database=20M?= =?UTF-8?q?igrations=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Resolves #40040. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas --- guides/source/getting_started.md | 112 +++++++++++++++---------------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 83ac14dc00..a9e4812fbc 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -410,6 +410,61 @@ The two files we'll focus on are the migration file (`db/migrate/_create_articles.rb`) and the model file (`app/models/article.rb`). +### Database Migrations + +*Migrations* are used to alter the structure of an application's database. In +Rails applications, migrations are written in Ruby so that they can be +database-agnostic. + +Let's take a look at the contents of our new migration file: + +```ruby +class CreateArticles < ActiveRecord::Migration[6.0] + def change + create_table :articles do |t| + t.string :title + t.text :body + + t.timestamps + end + end +end +``` + +The call to `create_table` specifies how the `articles` table should be +constructed. By default, the `create_table` method adds an `id` column as an +auto-incrementing primary key. So the first record in the table will have an +`id` of 1, the next record will have an `id` of 2, and so on. + +Inside the block for `create_table`, two columns are defined: `title` and +`body`. These were added by the generator because we included them in our +generate command (`bin/rails generate model Article title:string body:text`). + +On the last line of the block is a call to `t.timestamps`. This method defines +two additional columns named `created_at` and `updated_at`. As we will see, +Rails will manage these for us, setting the values when we create or update a +model object. + +Let's run our migration with the following command: + +```bash +$ bin/rails db:migrate +``` + +The command will display output indicating that the table was created: + +``` +== CreateArticles: migrating =================================== +-- create_table(:articles) + -> 0.0018s +== CreateArticles: migrated (0.0018s) ========================== +``` + +TIP: To learn more about migrations, see [Active Record Migrations]( +active_record_migrations.html). + +Now we can interact with the table using our model. + Getting Up and Running ---------------------- @@ -718,63 +773,6 @@ This action is now displaying the parameters for the article that are coming in from the form. However, this isn't really all that helpful. Yes, you can see the parameters but nothing in particular is being done with them. -### Running a Migration - -As we've just seen, `bin/rails generate model` created a _database migration_ file -inside the `db/migrate` directory. Migrations are Ruby classes that are -designed to create and modify database tables. Rails uses -rake commands to run migrations, and it's possible to undo a migration after -it's been applied to your database. Migration filenames include a timestamp to -ensure that they're processed in the order that they were created. - -If you look in the `db/migrate/YYYYMMDDHHMMSS_create_articles.rb` file -(remember, yours will have a slightly different name), here's what you'll find: - -```ruby -class CreateArticles < ActiveRecord::Migration[6.0] - def change - create_table :articles do |t| - t.string :title - t.text :text - - t.timestamps - end - end -end -``` - -The above migration creates a method named `change` which will be called when -you run this migration. The action defined in this method is also reversible, -which means Rails knows how to reverse the change made by this migration, -in case you want to reverse it later. When you run this migration it will create -an `articles` table with one string column and a text column. It also creates -two timestamp fields to allow Rails to track article creation and update times. - -TIP: For more information about migrations, refer to [Active Record Migrations] -(active_record_migrations.html). - -At this point, you can use a rails command to run the migration: - -```bash -$ bin/rails db:migrate -``` - -Rails will execute this migration command and tell you it created the Articles -table. - -``` -== CreateArticles: migrating ================================================== --- create_table(:articles) - -> 0.0019s -== CreateArticles: migrated (0.0020s) ========================================= -``` - -NOTE. Because you're working in the development environment by default, this -command will apply to the database defined in the `development` section of your -`config/database.yml` file. If you would like to execute migrations in another -environment, for instance in production, you must explicitly pass it when -invoking the command: `bin/rails db:migrate RAILS_ENV=production`. - ### Saving Data in the Controller Back in `ArticlesController`, we need to change the `create` action From 8312f2c0042a3f499436ec88298e17af927240d0 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:39:31 -0500 Subject: [PATCH 07/21] =?UTF-8?q?Add=20GS=20guide=20=C2=A7=20Using=20a=20M?= =?UTF-8?q?odel=20with=20the=20Database=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Resolves #40040. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas --- guides/source/getting_started.md | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index a9e4812fbc..b2640a4e31 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -465,6 +465,85 @@ active_record_migrations.html). Now we can interact with the table using our model. +### Using a Model to Interact with the Database + +To play with our model a bit, we're going to use a feature of Rails called the +*console*. The console is an interactive coding environment just like `irb`, but +it also automatically loads Rails and our application code. + +Let's launch the console with this command: + +```bash +$ bin/rails console +``` + +You should see an `irb` prompt like: + +```irb +Loading development environment (Rails 6.0.2.1) +irb(main):001:0> +``` + +At this prompt, we can initialize a new `Article` object: + +```irb +irb> article = Article.new(title: "Hello Rails", body: "I am on Rails!") +``` + +It's important to note that we have only *initialized* this object. This object +is not saved to the database at all. It's only available in the console at the +moment. To save the object to the database, we must call [`save`]( +https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-save): + +```irb +irb> article.save +(0.1ms) begin transaction +Article Create (0.4ms) INSERT INTO "articles" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Hello Rails"], ["body", "I am on Rails!"], ["created_at", "2020-01-18 23:47:30.734416"], ["updated_at", "2020-01-18 23:47:30.734416"]] +(0.9ms) commit transaction +=> true +``` + +The above output shows an `INSERT INTO "articles" ...` database query. This +indicates that the article has been inserted into our table. And if we take a +look at the `article` object again, we see something interesting has happened: + +```irb +irb> article +=> #
+``` + +The `id`, `created_at`, and `updated_at` attributes of the object are now set. +Rails did this for us when we saved the object. + +When we want to fetch this article from the database, we can call [`find`]( +https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find) +on the model and pass the `id` as an argument: + +```irb +irb> Article.find(1) +=> #
+``` + +And when we want to fetch all articles from the database, we can call [`all`]( +https://api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-all) +on the model: + +```irb +irb> Article.all +=> #]> +``` + +This method returns an [`ActiveRecord::Relation`]( +https://api.rubyonrails.org/classes/ActiveRecord/Relation.html) object, which +you can think of as a super-powered array. + +TIP: To learn more about models, see [Active Record Basics]( +active_record_basics.html) and [Active Record Query Interface]( +active_record_querying.html). + +Models are the final piece of the MVC puzzle. Next, we will connect all of the +pieces together. + Getting Up and Running ---------------------- From f3222cb7acc9993cc1dbbe08fb0c5a5640dd1167 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:42:21 -0500 Subject: [PATCH 08/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Showing=20a?= =?UTF-8?q?=20List=20of=20Articles=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas --- guides/source/getting_started.md | 112 +++++++++++++++---------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index b2640a4e31..a095152c96 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -544,6 +544,62 @@ active_record_querying.html). Models are the final piece of the MVC puzzle. Next, we will connect all of the pieces together. +### Showing a List of Articles + +Let's go back to our controller in `app/controllers/articles_controller.rb`, and +change the `index` action to fetch all articles from the database: + +```ruby +class ArticlesController < ApplicationController + def index + @articles = Article.all + end +end +``` + +Controller instance variables can be accessed by the view. That means we can +reference `@articles` in `app/views/articles/index.html.erb`. Let's open that +file, and replace its contents with: + +```html+erb +

Articles

+ +
    + <% @articles.each do |article| %> +
  • + <%= article.title %> +
  • + <% end %> +
+``` + +The above code is a mixture of HTML and *ERB*. ERB is a templating system that +evaluates Ruby code embedded in a document. Here, we can see two types of ERB +tags: `<% %>` and `<%= %>`. The `<% %>` tag means "evaluate the enclosed Ruby +code." The `<%= %>` tag means "evaluate the enclosed Ruby code, and output the +value it returns." Anything you could write in a regular Ruby program can go +inside these ERB tags, though it's usually best to keep the contents of ERB tags +short, for readability. + +Since we don't want to output the value returned by `@articles.each`, we've +enclosed that code in `<% %>`. But, since we *do* want to output the value +returned by `article.title` (for each article), we've enclosed that code in +`<%= %>`. + +We can see the final result by visiting . (Remember that +`bin/rails server` must be running!) Here's what happens when we do that: + +1. The browser makes a request: `GET http://localhost:3000`. +2. Our Rails application receives this request. +3. The Rails router maps the root route to the `index` action of `ArticlesController`. +4. The `index` action uses the `Article` model to fetch all articles in the database. +5. Rails automatically renders the `app/views/articles/index.html.erb` view. +6. The ERB code in the view is evaluated to output HTML. +7. The server sends a response containing the HTML back to the browser. + +We've connected all the MVC pieces together, and we have our first controller +action! Next, we'll move on to the second action. + Getting Up and Running ---------------------- @@ -997,62 +1053,6 @@ Visit and give it a try! ![Show action for articles](images/getting_started/show_action_for_articles.png) -### Listing all Articles - -We still need a way to list all our articles, so let's do that. -The route for this as per output of `bin/rails routes` is: - -``` -articles GET /articles(.:format) articles#index -``` - -Add the corresponding `index` action for that route inside the -`ArticlesController` in the `app/controllers/articles_controller.rb` file. -When we write an `index` action, the usual practice is to place it as the -first method in the controller. Let's do it: - -```ruby -class ArticlesController < ApplicationController - def index - @articles = Article.all - end - - def show - @article = Article.find(params[:id]) - end - - def new - end - - # snippet for brevity -``` - -And then finally, add the view for this action, located at -`app/views/articles/index.html.erb`: - -```html+erb -

Listing Articles

- - - - - - - - - <% @articles.each do |article| %> - - - - - - <% end %> -
TitleText
<%= article.title %><%= article.text %><%= link_to 'Show', article_path(article) %>
-``` - -Now if you go to you will see a list of all the -articles that you have created. - ### Adding Links You can now create, show, and list articles. Now let's add some links to From d89123d771d11364d2dcb311e528abf1a945bc45 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:43:40 -0500 Subject: [PATCH 09/21] =?UTF-8?q?Add=20GS=20guide=20=C2=A7=20CRUDit=20Wher?= =?UTF-8?q?e=20CRUDit=20Is=20Due=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- guides/source/getting_started.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index a095152c96..9ca4d2d3a4 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -600,11 +600,16 @@ We can see the final result by visiting . (Remember that We've connected all the MVC pieces together, and we have our first controller action! Next, we'll move on to the second action. -Getting Up and Running ----------------------- +CRUDit Where CRUDit Is Due +-------------------------- -Now that you've seen how to create a controller, an action, and a view, let's -create something with a bit more substance. +Almost all web applications involve [CRUD (Create, Read, Update, and Delete)]( +https://en.wikipedia.org/wiki/Create,_read,_update,_and_delete) operations. You +may even find that the majority of the work your application does is CRUD. Rails +acknowledges this, and provides many features to help simplify code doing CRUD. + +Let's begin exploring these features by adding more functionality to our +application. 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 articles, people, or From 69979b9ad14716d18e9992f18f98748996ee83ea Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:45:26 -0500 Subject: [PATCH 10/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Showing=20a?= =?UTF-8?q?=20Single=20Article=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus Co-Authored-By: Tasos Latsas --- .../show_action_for_articles.png | Bin 2901 -> 0 bytes guides/source/getting_started.md | 201 +++++++----------- 2 files changed, 73 insertions(+), 128 deletions(-) delete mode 100644 guides/assets/images/getting_started/show_action_for_articles.png diff --git a/guides/assets/images/getting_started/show_action_for_articles.png b/guides/assets/images/getting_started/show_action_for_articles.png deleted file mode 100644 index 68837131f7c0f9b23115f36616157baead071e20..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2901 zcmZ8jXHXLi5>4m?QB<1JB2o>#hAv2y&{29wL`phw$u&^zGBg&qVnj>{>9Ydvhn+g_u|!|W*DLKg3ce$DRt6~ zldI}G9y>K{-w{<(b=MA__fqY-6tn`8ly!Nf=T4E0O6LM1 zc6N3~)1uaqvS)IAO$4(tva)tu>w`MjopY$16UT0YvRcmg>f8*Mk9q-Gs=cyurgDe#D^F0WySfT)3QZSIh2Z1)tn6pKqrjg>mD zF?K9CSvWVOj)Hukf}xza7{;ykLs_H9{Ir_W_`{Z6Fe2rer+O?+D1+K)sd>H)uAi{W zwBwwInR@NJq{`_L#gK2x`o4#|_b1<9SX)4y3S4&Bz}=$-_&kKlw2usf@_IuN^m_)D znEYamKE>8&aw;XBt)|%iOv!O3fDbavm(?eB-AYlaISL)bs@8U~_scFXZF0swuypJbRQUQ{y{ ze_H5xK?s!T@-e^S%drIA!jOoD#+rTFSDnPuDN;a3t_rmwzyLLEKnJzq#wC7b5?N_` zQUPJj+^r2W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dQv`$Llz;^=wzXRG(!py?-z zaG`%{E1{ihmlZ76m#55~WMZKkA2LvHr*qMpr}`z@ll_e8#`+)OLz$PHJ?etL55_l6 zYF*`II=9;ra^51LVbpf<%h^8d)^fCbZbcCulf5@PY4GXK#nP8G_oc)j+-}R5xw=Rm zy_<3+!^N7aC3UyyVp@OaJRq)2nZ6G~`@VpQc#ER`QE>jlwPI|J3@giH(pHAr{YnnK zy=TBwLV)-y02x#>=a8H9+|(`CRdd2nlie>v7UqRczR>X0r(fqIsCkT`Y-4%D{69lE zI}UmI26Ke)!0y0GZSesjN}4uk3^5kxDJgZ0aJwplE)ne-5g8+ZN9$6Gf8bod`jsH5 z=$IxZmIg zSd^omenzc4hZDzgBYZa)FH@Fn3P!(n%TAr)BU_U}Ms+Frg45pG1l&mKh-=g}j)>08 z`W_}{2RXbx7FL1S_1ialdNKQ1B5qE5p^VejRAW*Zi$)`H@2b~eAchRqDGf|35Y&7h z-5o1b#WbttA53sv@*(G~d$HDLI*_KUM^2U$ l`~T1hI^zBtvi`^LasYXGHMzTaQjdfHaG0%mrI}~!zX1Jvsaya6 diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 9ca4d2d3a4..4f6d644971 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -611,6 +611,79 @@ acknowledges this, and provides many features to help simplify code doing CRUD. Let's begin exploring these features by adding more functionality to our application. +### Showing a Single Article + +We currently have a view that lists all articles in our database. Let's add a +new view that shows the title and body of a single article. + +We start by adding a new route that maps to a new controller action (which we +will add next). Open `config/routes.rb`, and insert the last route shown here: + +```ruby +Rails.application.routes.draw do + root "articles#index" + + get "/articles", to: "articles#index" + get "/articles/:id", to: "articles#show" +end +``` + +The new route is another `get` route, but it has something extra in its path: +`:id`. This designates a route *parameter*. A route parameter captures a segment +of the request's path, and puts that value into the `params` Hash, which is +accessible by the controller action. For example, when handling a request like +`GET http://localhost:3000/articles/1`, `1` would be captured as the value for +`:id`, which would then be accessible as `params[:id]` in the `show` action of +`ArticlesController`. + +Let's add that `show` action now, below the `index` action in +`app/controllers/articles_controller.rb`: + +```ruby +class ArticlesController < ApplicationController + def index + @articles = Article.all + end + + def show + @article = Article.find(params[:id]) + end +end +``` + +The `show` action calls `Article.find` ([mentioned +previously](#using-a-model-to-interact-with-the-database)) with the ID captured +by the route parameter. The returned article is stored in the `@article` +instance variable, so it is accessible by the view. By default, the `show` +action will render `app/views/articles/show.html.erb`. + +Let's create `app/views/articles/show.html.erb`, with the following contents: + +```html+erb +

<%= @article.title %>

+ +

<%= @article.body %>

+``` + +Now we can see the article when we visit ! + +To finish up, let's add a convenient way to get to an article's page. We'll link +each article's title in `app/views/articles/index.html.erb` to its page: + +```html+erb +

Articles

+ + +``` + 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 articles, people, or animals. @@ -993,134 +1066,6 @@ TIP: For more information, refer to the reference above and [this blog article about Strong Parameters] (https://weblog.rubyonrails.org/2012/3/21/strong-parameters/). -### Showing Articles - -If you submit the form again now, Rails will complain about not finding the -`show` action. That's not very useful though, so let's add the `show` action -before proceeding. - -As we have seen in the output of `bin/rails routes`, the route for `show` action is -as follows: - -``` -article GET /articles/:id(.:format) articles#show -``` - -The special syntax `:id` tells rails that this route expects an `:id` -parameter, which in our case will be the id of the article. - -As we did before, we need to add the `show` action in -`app/controllers/articles_controller.rb` and its respective view. - -NOTE: A frequent practice is to place the standard CRUD actions in each -controller in the following order: `index`, `show`, `new`, `edit`, `create`, `update` -and `destroy`. You may use any order you choose, but keep in mind that these -are public methods; as mentioned earlier in this guide, they must be placed -before declaring `private` visibility in the controller. - -Given that, let's add the `show` action, as follows: - -```ruby -class ArticlesController < ApplicationController - def show - @article = Article.find(params[:id]) - end - - def new - end - - # snippet for brevity -``` - -A couple of things to note. We use `Article.find` to find the article we're -interested in, passing in `params[:id]` to get the `:id` parameter from the -request. We also use an instance variable (prefixed with `@`) to hold a -reference to the article object. We do this because Rails will pass all instance -variables to the view. - -Now, create a new file `app/views/articles/show.html.erb` with the following -content: - -```html+erb -

- Title: - <%= @article.title %> -

- -

- Text: - <%= @article.text %> -

-``` - -With this change, you should finally be able to create new articles. -Visit and give it a try! - -![Show action for articles](images/getting_started/show_action_for_articles.png) - -### Adding Links - -You can now create, show, and list articles. Now let's add some links to -navigate through pages. - -Open `app/views/welcome/index.html.erb` and modify it as follows: - -```html+erb -

Hello, Rails!

-<%= link_to 'My Blog', controller: 'articles' %> -``` - -The `link_to` method is one of Rails' built-in view helpers. It creates a -hyperlink based on text to display and where to go - in this case, to the path -for articles. - -Let's add links to the other views as well, starting with adding this -"New Article" link to `app/views/articles/index.html.erb`, placing it above the -`` tag: - -```erb -<%= link_to 'New article', new_article_path %> -``` - -This link will allow you to bring up the form that lets you create a new article. - -Now, add another link in `app/views/articles/new.html.erb`, underneath the -form, to go back to the `index` action: - -```erb -<%= form_with scope: :article, url: articles_path, local: true do |form| %> - ... -<% end %> - -<%= link_to 'Back', articles_path %> -``` - -Finally, add a link to the `app/views/articles/show.html.erb` template to -go back to the `index` action as well, so that people who are viewing a single -article can go back and view the whole list again: - -```html+erb -

- Title: - <%= @article.title %> -

- -

- Text: - <%= @article.text %> -

- -<%= link_to 'Back', articles_path %> -``` - -TIP: If you want to link to an action in the same controller, you don't need to -specify the `:controller` option, as Rails will use the current controller by -default. - -TIP: In the development environment (which is what you're working in by -default), Rails reloads your application with every browser request, so there's -no need to stop and restart the web server when a change is made. - ### Adding Some Validation The model file, `app/models/article.rb` is about as simple as it can get: From fa64129117c90d742c8e6d371417e5ec14935ebf Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:54:45 -0500 Subject: [PATCH 11/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Resourceful?= =?UTF-8?q?=20Routing=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38288. Co-Authored-By: Ryan Bigg Co-Authored-By: Eileen M. Uchitelle Co-Authored-By: Petrik de Heus --- .../images/getting_started/new_article.png | Bin 3193 -> 0 bytes guides/source/getting_started.md | 100 ++++++++++++------ 2 files changed, 68 insertions(+), 32 deletions(-) delete mode 100644 guides/assets/images/getting_started/new_article.png diff --git a/guides/assets/images/getting_started/new_article.png b/guides/assets/images/getting_started/new_article.png deleted file mode 100644 index 6edcc161b6b702274146e689f46dcca01e20ec36..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3193 zcmds4c{mi@8lM?682erZC1LE!ZmeS`M$L#KTMZ_ANLiAZN_Jx}!&pL5A-)jF$d(i; zOM^5CnJ-c?#+p0x-TU2t?$dql-}iaW^8ViUyyu+f{k`Wr=cF7*+4A$icmMzZzrCFm z8UO$iSapG$r2u%WjY|RmeCGC6NSDjNr8h@`ECiC>E{s0902H^2sWt~H^N5w11Ao_a z$w8D1el7u@<(uIvHfla}hDK^NP6Fo`%QW_JTwOiGlYtAte8=k)L_=!>Xd>Y6n)p!Po`0A}&a!y6;bQd9m zCmR~RhAg7Jtu-fXZn<^v9u^zWyleI2zQvp^2V5onM`vp>T2*vJndnpg%Uay_`MK0N zJ)dNYE07Zh|Pn(awRn=d>Xx$r8a@kEY5c=nEeTV7DuHq(16;#A5Rm4G&R ziyf;$)D!ih#Yb+3))^a&&mZHklk!I#FNT81P$%MUf^9%WNhE- zZ2IF`}vC>%`Nbwu=lb}J64xhbPOK2cy06mqH@-EN}!z9!ocM=V4n z9$bv@Ko=S$yC2@=R4Yt?aJGB*xW0J6b!2y6*;O0?6T6THikkOQd- zEn|^ihC1N#T?H<*>!@v2z42q5e@cq%uSV$NVH|MrZ4qfZLS;td9{|%_8xGIPsYJIb zCQXgnDwtV6DGgy>mv5D|)R@W)R?Rw3v8?@cg9xq%d|Ar%I}aOnkz&uIhosUeE6>25 ziNm0YCXL*6;L3ets6m^9U>AqhVDgQuDw1q$z;QwkBfEiJu%F4TQM!TqFY3fxz8 z#~z1c`*6W;aanoJ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d4eUrOK4Id`YuVYCjECKd+#vrTu zT#S&TdH+i`{qK4Ae>{?qGsS_VgSZ{`nICTh5E>!tfAw!6Lg}fi1Q>?%$u#u0=kc$& zAVmltsk&UZ24a=?n0ph=Ax7>~JmbwFad#fG4*l|@3vcR~LkV9Es57X5hl&})k#FfF zddduZXpFg3fD+MBtMD0wZ!Y1Jti_ggHjrCO*V)o#%~F|>hGfF>%3PBSOalFV;o%w( zV!_j_7`Y^~TK#Hz&gSRJ>5KyTvmCKP!^T&-U2&)EkV_ZiYn>hof1LcKvm08=|1j?b zq%#g_7R;jzfRN7l$G^3`jBwQmsj+O{i}(M2mGls~g-H0dH;=Zj8tQk{of?hO6@uBO z>64^;#-aR5kCz-3Ll%T@X0w8tfkjVbWt+no%CX zYRkd<d`($zOb$% zDPj?#gy1!y=m;v*H$^i-57{lpqcXv%x37# z2kk*-{aac^`E41i>NQRusSj?lhRB`|S diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 4f6d644971..850ed84547 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -684,54 +684,90 @@ each article's title in `app/views/articles/index.html.erb` to its page: ``` -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 articles, people, or -animals. -You can create, read, update, and destroy items for a resource and these -operations are referred to as _CRUD_ operations. +### Resourceful Routing -Rails provides a `resources` method which can be used to declare a standard REST -resource. You need to add the _article resource_ to the -`config/routes.rb` so the file will look as follows: +So far, we've covered the "R" (Read) of CRUD. We will eventually cover the "C" +(Create), "U" (Update), and "D" (Delete). As you might have guessed, we will do +so by adding new routes, controller actions, and views. Whenever we have such a +combination of routes, controller actions, and views that work together to +perform CRUD operations on an entity, we call that entity a *resource*. For +example, in our application, we would say an article is a resource. + +Rails provides a routes method named [`resources`]( +https://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html#method-i-resources) +that maps all of the conventional routes for a collection of resources, such as +articles. So before we proceed to the "C", "U", and "D" sections, let's replace +the two `get` routes in `config/routes.rb` with `resources`: ```ruby Rails.application.routes.draw do - get 'welcome/index' + root "articles#index" resources :articles - - root 'welcome#index' end ``` -If you run `bin/rails routes`, you'll see that it has defined routes for all the -standard RESTful actions. The meaning of the prefix column (and other columns) -will be seen later, but for now notice that Rails has inferred the -singular form `article` and makes meaningful use of the distinction. +We can inspect what routes are mapped by running the `bin/rails routes` command: ```bash $ bin/rails routes - Prefix Verb URI Pattern Controller#Action -welcome_index GET /welcome/index(.:format) welcome#index - articles GET /articles(.:format) articles#index - POST /articles(.:format) articles#create - new_article GET /articles/new(.:format) articles#new - edit_article GET /articles/:id/edit(.:format) articles#edit - article GET /articles/:id(.:format) articles#show - PATCH /articles/:id(.:format) articles#update - PUT /articles/:id(.:format) articles#update - DELETE /articles/:id(.:format) articles#destroy - root GET / welcome#index + Prefix Verb URI Pattern Controller#Action + root GET / articles#index + articles GET /articles(.:format) articles#index + new_article GET /articles/new(.:format) articles#new + article GET /articles/:id(.:format) articles#show + POST /articles(.:format) articles#create +edit_article GET /articles/:id/edit(.:format) articles#edit + PATCH /articles/:id(.:format) articles#update + DELETE /articles/:id(.:format) articles#destroy ``` -In the next section, you will add the ability to create new articles in your -application and be able to view them. This is the "C" and the "R" from CRUD: -create and read. The form for doing this will look like this: +The `resources` method also sets up URL and path helper methods that we can use +to keep our code from depending on a specific route configuration. The values +in the "Prefix" column above plus a suffix of `_url` or `_path` form the names +of these helpers. For example, the `article_path` helper returns +`"/articles/#{article.id}"` when given an article. We can use it to tidy up our +links in `app/views/articles/index.html.erb`: -![The new article form](images/getting_started/new_article.png) +```html+erb +

Articles

-It will look a little basic for now, but that's ok. We'll look at improving the -styling for it afterwards. + +``` + +However, we will take this one step further by using the [`link_to`]( +https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to) +helper. The `link_to` helper renders a link with its first argument as the +link's text and its second argument as the link's destination. If we pass a +model object as the second argument, `link_to` will call the appropriate path +helper to convert the object to a path. For example, if we pass an article, +`link_to` will call `article_path`. So `app/views/articles/index.html.erb` +becomes: + +```html+erb +

Articles

+ +
    + <% @articles.each do |article| %> +
  • + <%= link_to article.title, article %> +
  • + <% end %> +
+``` + +Nice! + +TIP: To learn more about routing, see [Rails Routing from the Outside In]( +routing.html). ### Laying down the Groundwork From bdb2b7967fd5b7eb70135bc50c6445287daea1ee Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:56:01 -0500 Subject: [PATCH 12/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Creating=20a?= =?UTF-8?q?=20New=20Article=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38328. Co-Authored-By: Ryan Bigg --- .../routing_error_no_controller.png | Bin 3869 -> 0 bytes .../template_is_missing_articles_new.png | Bin 21220 -> 0 bytes .../unknown_action_new_for_articles.png | Bin 4933 -> 0 bytes guides/source/getting_started.md | 188 ++++-------------- 4 files changed, 41 insertions(+), 147 deletions(-) delete mode 100644 guides/assets/images/getting_started/routing_error_no_controller.png delete mode 100644 guides/assets/images/getting_started/template_is_missing_articles_new.png delete mode 100644 guides/assets/images/getting_started/unknown_action_new_for_articles.png diff --git a/guides/assets/images/getting_started/routing_error_no_controller.png b/guides/assets/images/getting_started/routing_error_no_controller.png deleted file mode 100644 index 52150f0426026f2387326a78d6ce588b889fd89c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3869 zcmZ8k2T&8*wha)aDMh4rL8%u+z$j6WNDB~(6zNr(N@58}%!NScRfvK>1jIz}0x>k{ zB}4=f5HNH|0w_pH1f?nnk9+@{|K9&IXZD=6_nb3(&pK!CS;=-b=EwP@_y7RFaj1pK zH2{F4^>81|!+B_z-#3B*0D&)16Jy5+4%Tpo5CCwDtFnV-go#Eu6$2XiuPz|afCv!^ z9>4&uorwlRj_em z9%f(*UO_i}eO9LY#(Ar(?EbQW65XTH!*_Xe55DOg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IW diff --git a/guides/assets/images/getting_started/template_is_missing_articles_new.png b/guides/assets/images/getting_started/template_is_missing_articles_new.png deleted file mode 100644 index 8adcfcf0d728dfa00a077f3754c24a51b363a669..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 21220 zcmc$`1yodF*e;47A`F5eDJfFIP|^)5U4nE=cXtXR2uMhGN`nJ~bW2DhATe}@44`xl z_e1}Gopb(s?mB1Pd)B&J95;KI&ED_##`8SyyA6A#Bz+I(5e^y}+C5pBm#S!J=y0%q zb{8GIs*y{mfPZdVNGM96p;bmhF5h5+?`ceAR29+Cycy8Y-Ug$gor6PfH__1CIMC33 z8KI#GCZnN|IHotK2!l877|Tn)M7zHEligex2aaGn%4oZwp+O$r{Jn*inof#_rm`;k zQbNshdMD$|kWdw!l~0j!THFlxyNo#4 z$l1BO@0@Qft7x?C9 zgkGVtS?$CExJg{(J7z{i7W660%Bl8vbVEX|xyOl*~&t0s{{XvO_$s0)2I`Y;OAgM~qYjE#H{+jV_Y1AoKX?|O@NX|Z^Iy|~B z_7yq*rp3eO=MPR!!DrK7_Sny|P^S;fA4ioTC`_i6DJ)R7(=~xyu-^*VS$%p&GEA=T z_Xk*-ugxz7lU(z2&Zj4?sF`hD-xpf1CI0;0JbtwEzP<0eN%v6Lr%x*SHrMY6I<|&w zg8UM`6WpxUK+K5CfzW=3CyE6js#j^OeNx>YN9$_XO3Tp%9~D2t2<*_>UP-mE zb?=$=W>JJTtH%2wSc|XHGA=$JJ$=-s+pP2Xx%G=HOqU;5ED462Mv`6$3)}D)X`I(( z$uC0E9x?kRHP$ZA&J;B_2T5{af?eLtchr; zx9dl`LI?HX;`Q_e*O??XExmvK9G98MJ;KDshKu|66h+=mrJ&AAPfsT$FUP(Yv)eH@ z33F_m?YzCgy#j|8Mc+#!^>dH*{d~0L3Df@ll9qodCpeS*eUN~{$4H!AhZesV-j+V+ z!x?%3A0tIJVpy)-9J(?RFo$@_E)1`t`;r$RpD^QyAq&r?LfBN*96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1~Q1pP3j&}i4yt5@&L{g*UFTDPv1 zVMVOS5R@jYdC!%08v$3UA@!>qFXfPQPBn44+?avbtnGMSe5s{H8we326loHwn;ckP zt3b8LgV7ObvgpdT>vRU=E__!~;#Ew+pwYJ$;{vE7w&lFJSRNOwcI(8%gxVey*uZ*< zsFyycKlDqFR(=H&83Uq<(bkZJS!u|`j7aqPnckkCvG%5*-T6hfHa6U|na-80j;TT| zJv~uLPM#7csSRz%AyvXv6sLFm?E-RDkAepA<;$1;vC}(fG~F7c(NPU5SVWKh#YB-< zgtt35+zy5au$zLT3dlBqsV(upoVC9Jz(gjCyL4ZWkr4$FohMa6a(Z}pjW^y;UU%rS}NipkhVsQL2bP8s^WZ-S=*)EKQI z{M?i9wVf2GiYN>Qcm#l(47HPTjF9{QiA0JI|M(ZcYf+A$9ldfvM>~?1xMU<)qgst~ zI+Kq85Fq(E=2$wp5nr|wuON@HUIyF|V;fx$3PtOWkVecrgCmH7`UWW}nQ3HZu+Mo`6K)0*``!z^3C#s)^IvY;<`3HOPq(;1Y{84!nS*4q zkQ3O?%|wj1L+xMnEB3hk4sO)r6B@;wgfn@>>!o8fDs~B+ri{P+-Sk**FSU+$l0`>( zy0Eh>QE~3-X!O&_fZ5M(>r6(;%)qfco3aFPpk-51apG5InmaK wKy)BlJFK@hBM=-2gy}~wwtVzJSOg;^_|X4;ATi1NoqWEoTRokxk?EY@0K#@%3;+NC diff --git a/guides/assets/images/getting_started/unknown_action_new_for_articles.png b/guides/assets/images/getting_started/unknown_action_new_for_articles.png deleted file mode 100644 index f7e7464d61bfc488c786085903f16b5e053621d3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4933 zcmai2cTkhf*A0Xs(m|?H1w;YC1Q6-HN(sFvNDIZ#JE1p0N>D=y5UPMQl@2ORAxNYb zA#|ko-a)?jI`2Eb|GsC=JhOA}&fML*XU?A4I6WOT%ImDx0RRA{hPtvo06?gWANyV- z#Q%#v+>8JK=qWUm6%73eHb%m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!x-mrsW-N1hj- zhBxT<4=q&H%A9^Y`-Rbr=iXMKx)q@&0?7B#M$yvRFY-eYnSv)CHq}h}sX5!0i+faQ z1f`Qmun)K&N$JOtXlYu&R|7PMLw!u1ZUoDhwX11VzdK|grp&AmeFn8%=~Q#ky?oK4 zZjc%`Q7m^?i`DNP-J(1y0YYr;NDh0P;B9wsf19aToG+ZPdQ& zTsK?6%v4ND#^(r zn-Md2FkN>EWQnN9OnGsyLsw(+RL|oCL`1#E{eRTPwh4HjSw%dk`Eyo`Y}c&g4ec(i z!y<(~(98*Dz?^dx`rCD|Sp-?O;s2Z$jr?mU{b6lad-9rQqsq<9yr$_MGLB{*4Q!{G zNiN4dypm7Wk+og(@{Azx3hLO)qmj++=68&2fWJyq@-J4PxvX!JWW>K!0hkxuWD;a} z2Xa^dLH}c!@wWJ1%Z#_h|5|3ehsN9Ke?9bn{`&u)OxRcpF(1uGn*S-Op`xSwUh!GP F{{eyLQV#$C diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 850ed84547..bfc907b1fa 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -769,115 +769,61 @@ Nice! TIP: To learn more about routing, see [Rails Routing from the Outside In]( routing.html). -### Laying down the Groundwork +### Creating a New Article -Firstly, you need a place within the application to create a new article. A -great place for that would be at `/articles/new`. With the route already -defined, requests can now be made to `/articles/new` in the application. -Navigate to and you'll see a routing -error: +Now we move on to the "C" (Create) of CRUD. Typically, in web applications, +creating a new resource is a multi-step process. First, the user requests a form +to fill out. Then, the user submits the form. If there are no errors, then the +resource is created and some kind of confirmation is displayed. Else, the form +is redisplayed with error messages, and the process is repeated. -![Another routing error, uninitialized constant ArticlesController](images/getting_started/routing_error_no_controller.png) - -This error occurs because the route needs to have a controller defined in order -to serve the request. The solution to this particular problem is to create -a controller called `ArticlesController`. You can do this by running this -command: - -```bash -$ bin/rails generate controller Articles -``` - -If you open up the newly generated `app/controllers/articles_controller.rb` -you'll see a fairly empty controller: +In a Rails application, these steps are conventionally handled by a controller's +`new` and `create` actions. Let's add a typical implementation of these actions +to `app/controllers/articles_controller.rb`, below the `show` action: ```ruby class ArticlesController < ApplicationController -end -``` + def index + @articles = Article.all + end -A controller is 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 articles -within our system. + def show + @article = Article.find(params[:id]) + end -NOTE: There are `public`, `private` and `protected` methods in Ruby, -but only `public` methods can be actions for controllers. -For more details check out [Programming Ruby](https://ruby-doc.org/docs/ProgrammingRuby/). - -If you refresh now, you'll get a new error: - -![Unknown action new for ArticlesController!](images/getting_started/unknown_action_new_for_articles.png) - -This error indicates that Rails cannot find the `new` action inside the -`ArticlesController` that you just generated. This is because when controllers -are generated in Rails they are empty by default, unless you tell it -your desired 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/articles_controller.rb` and inside the `ArticlesController` -class, define the `new` method so that your controller now looks like this: - -```ruby -class ArticlesController < ApplicationController def new + @article = Article.new + end + + def create + @article = Article.new(title: "...", body: "...") + + if @article.save + redirect_to @article + else + render :new + end end end ``` -With the `new` method defined in `ArticlesController`, if you refresh - you'll see another error: +The `new` action instantiates a new article, but does not save it. This article +will be used in the view when building the form. By default, the `new` action +will render `app/views/articles/new.html.erb`, which we will create next. -![Template is missing for articles/new] -(images/getting_started/template_is_missing_articles_new.png) +The `create` action instantiates a new article with values for the title and +body, and attempts to save it. If the article is saved successfully, the action +redirects the browser to the article's page at `"http://localhost:3000/articles/#{@article.id}"`. +Else, the action redisplays the form by rendering `app/views/articles/new.html.erb`. +The title and body here are dummy values. After we create the form, we will come +back and change these. -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 will raise an exception. - -Let's look at the full error message again: - ->ArticlesController#new is missing a template for request formats: text/html - ->NOTE! ->Unless told otherwise, Rails expects an action to render a template with the same name, contained in a folder named after its controller. If this controller is an API responding with 204 (No Content), which does not require a template, then this error will occur when trying to access it via browser, since we expect an HTML template to be rendered for such requests. If that's the case, carry on. - -The message identifies which template is missing. In this case, it's the -`articles/new` template. Rails will first look for this template. If not found, -then it will attempt to load a template called `application/new`, because the -`ArticlesController` inherits from `ApplicationController`. - -Next the message contains `request.formats` which specifies the format of -template to be served in response. It is set to `text/html` as we requested -this page via browser, so Rails is looking for an HTML template. - -The simplest template that would work in this case would be one located at -`app/views/articles/new.html.erb`. The extension of this file name is important: -the first extension is the _format_ of the template, and the second extension -is the _handler_ that will be used to render the template. Rails is attempting -to find a template called `articles/new` within `app/views` for the -application. The format for this template can only be `html` and the default -handler for HTML is `erb`. Rails uses other handlers for other formats. -`builder` handler is used to build XML templates and `coffee` handler uses -CoffeeScript to build JavaScript templates. Since you want to create a new -HTML form, you will be using the `ERB` language which is designed to embed Ruby -in HTML. - -Therefore the file should be called `articles/new.html.erb` and needs to be -located inside the `app/views` directory of the application. - -Go ahead now and create a new file at `app/views/articles/new.html.erb` and -write this content in it: - -```html -

New Article

-``` - -When you refresh 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 article. +NOTE: [`redirect_to`](https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to) +will cause the browser to make a new request, +whereas [`render`](https://api.rubyonrails.org/classes/AbstractController/Rendering.html#method-i-render) +renders the specified view for the current request. +It is important to use `redirect_to` after mutating the database or application state. +Otherwise, if the user refreshes the page, the browser will make the same request, and the mutation will be repeated. ### The first form @@ -970,58 +916,6 @@ NOTE: By default `form_with` submits forms using Ajax thereby skipping full page redirects. To make this guide easier to get into we've disabled that with `local: true` for now. -### Creating Articles - -To make the "Unknown action" go away, you can define a `create` action within -the `ArticlesController` class in `app/controllers/articles_controller.rb`, -underneath the `new` action, as shown: - -```ruby -class ArticlesController < ApplicationController - def new - end - - def create - end -end -``` - -If you re-submit the form now, you may not see any change on the page. Don't worry! -This is because Rails by default returns `204 No Content` response for an action if -we don't specify what the response should be. We added the `create` action -but didn't specify anything about how the response should be. In this case, the -`create` action should save our new article to the 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: - -```ruby -def create - render plain: params[:article].inspect -end -``` - -The `render` method here is taking a hash with a key of `:plain` and -value of `params[:article].inspect`. The `params` method is the object which -represents the parameters (or fields) coming in from the form. The `params` -method returns an `ActionController::Parameters` object, which -allows you to access the keys of the hash using either strings or symbols. In -this situation, the only parameters that matter are the ones from the form. - -TIP: Ensure you have a firm grasp of the `params` method, as you'll use it fairly regularly. Let's consider an example URL: **http://www.example.com/?username=dhh&email=dhh@email.com**. In this URL, `params[:username]` would equal "dhh" and `params[:email]` would equal "dhh@email.com". - -If you re-submit the form one more time, you'll see something that looks like the following: - -```ruby -"First Article!", "text"=>"This is my first article."} permitted: false> -``` - -This action is now displaying the parameters for the article that are coming in -from the form. However, this isn't really all that helpful. Yes, you can see the -parameters but nothing in particular is being done with them. - ### Saving Data in the Controller Back in `ArticlesController`, we need to change the `create` action From 0079c3ff55becf3e815d22ce62450ce9702080c3 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:57:45 -0500 Subject: [PATCH 13/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Using=20a=20?= =?UTF-8?q?Form=20Builder=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38328. Co-Authored-By: Ryan Bigg --- .../unknown_action_create_for_articles.png | Bin 14154 -> 0 bytes guides/source/getting_started.md | 116 +++++++----------- 2 files changed, 44 insertions(+), 72 deletions(-) delete mode 100644 guides/assets/images/getting_started/unknown_action_create_for_articles.png diff --git a/guides/assets/images/getting_started/unknown_action_create_for_articles.png b/guides/assets/images/getting_started/unknown_action_create_for_articles.png deleted file mode 100644 index 7b385ef9daece9a0e0ce4f9f3976cb5ba97af5a1..0000000000000000000000000000000000000000 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c`>pE$8C{{^W?29V_sEpBpws&JZt*@M83A{?Rl!*(Vqzj3F3ME>q(QpPb%YzQ z=5G1{Do@x-PEZzFXSP;c*xKpcLEj#!55Ba)lPMbP-3N2MK4<=VGrQ`G`X@q*+aS?J zX%Vh5N4OEr%1s%DmDo8xvUhpYdCUy{^|f{aC!(qqLQ4w_JaTJI7PN1O(|G7tmmi`x zaTN1U{l1+|j-~a#i3BCKlW4l9-wkJ$;05Gs1=`!>LB4{?#q>3Lv{#|JJ27FmvT}1U zZhO%Rj2jg7 z3^21Gr#K3Qh9&TNL@K(_RiSBH)GKR)wZiX!%1_L1(xwPKe!lg>(-)dtz3PTQQ>SzC z#Po_C-YmX&!>jngJ)r=)f29DzNt=6^U1%z(pL1&aXeRTTL2-#!T#xiv6giz1h~1+PM7mnr>0c&#dD;mQkGAY$rIisC%WlO7ZccrnSB@j zt#lD+qSR(BgK5z?atqoM@_Uzo;{1YU%}-`&I9xQ_>q2|F~wLS(Xdg%cuV+j2uX zJPM4~afUP9bgF6nJe!a2 z2^w_ztKF#Zi0fRDIBlRf_q{fVv{3F&=V6W>$_r3L1)~IrGk&pZgqA|cZKigfo4v7X zwHmrv4qePuMGdk)ZTDTid(KjamJT=WQ2(=r_`lQ>|FazVe=SY^59P@Jp)@%`^o9Z5 zB=72Fme~V+H!WB~3v6Z#HV2xzm_siBc2;&)CRPq6wl8X|Y(O>+AQuM%D=Uzd^=9cv z?Elli-qFn3!t?*$AUI6*4cg$-zc)a4=>kn1ojhHvEv>)+vH!LxM4X(U8+m0MOqm%e z*jd?lwdA-Kpl$H~yN#JS$kfH!32g1?01&qZIoTO|;u9yeKz{;|kx&q?5;Y3^e*hU+ Bh -

+

New Article

+ +<%= form_with model: @article, local: true do |form| %> +
<%= form.label :title %>
<%= form.text_field :title %> -

+
-

- <%= form.label :text %>
- <%= form.text_area :text %> -

+
+ <%= form.label :body %>
+ <%= form.text_area :body %> +
-

+

<%= form.submit %> -

+
<% end %> ``` -If you refresh the page now, you'll see the exact same form from our example above. +The [`form_with`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with) +helper method instantiates a form builder. In the `form_with` block we call +methods like [`label`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-label) +and [`text_field`](https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-text_field) +on the form builder to output the appropriate form elements. -When you call `form_with`, you pass it an identifying scope for this -form. In this case, it's the symbol `:article`. This tells the `form_with` -helper what this form is for. Inside the block for this method, the -`FormBuilder` object - represented by `form` - is used to build two labels and two -text fields, one each for the title and text of an article. Finally, a call to -`submit` on the `form` object will create a submit button for the form. +NOTE: By default, `form_with` creates a form that submits via Ajax to avoid full +page reloads. To make this guide easier to follow, we have disabled that feature +by using `local: true` in the above code. -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 `/articles/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 article. +The resulting output from our `form_with` call will look like: -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_with`. -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 that action. +```html +
+ -Edit the `form_with` line inside `app/views/articles/new.html.erb` to look like -this: +
+
+ +
-```html+erb -<%= form_with scope: :article, url: articles_path, local: true do |form| %> +
+
+ +
+ +
+ +
+ ``` -In this example, the `articles_path` helper is passed to the `:url` option. -To see what Rails will do with this, we look back at the output of -`bin/rails routes`: - -```bash -$ bin/rails routes - Prefix Verb URI Pattern Controller#Action -welcome_index GET /welcome/index(.:format) welcome#index - articles GET /articles(.:format) articles#index - POST /articles(.:format) articles#create - new_article GET /articles/new(.:format) articles#new - edit_article GET /articles/:id/edit(.:format) articles#edit - article GET /articles/:id(.:format) articles#show - PATCH /articles/:id(.:format) articles#update - PUT /articles/:id(.:format) articles#update - DELETE /articles/:id(.:format) articles#destroy - root GET / welcome#index -``` - -The `articles_path` helper tells Rails to point the form to the URI Pattern -associated with the `articles` prefix; and the form will (by default) send a -`POST` request to that route. This is associated with the `create` action of -the current controller, the `ArticlesController`. - -With the form and its associated route defined, you will be able to fill in the -form and then click the submit button to begin the process of creating a new -article, so go ahead and do that. When you submit the form, you should see a -familiar error: - -![Unknown action create for ArticlesController] -(images/getting_started/unknown_action_create_for_articles.png) - -You now need to create the `create` action within the `ArticlesController` for -this to work. - -NOTE: By default `form_with` submits forms using Ajax thereby skipping full page -redirects. To make this guide easier to get into we've disabled that with -`local: true` for now. +TIP: To learn more about form builders, see [Action View Form Helpers]( +form_helpers.html). ### Saving Data in the Controller From 0d04178c81fe437971c1a1c4d0e17812fb7a2613 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 16:58:46 -0500 Subject: [PATCH 14/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Using=20Stro?= =?UTF-8?q?ng=20Parameters=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38328. Co-Authored-By: Ryan Bigg --- .../forbidden_attributes_for_new_article.png | Bin 9851 -> 0 bytes guides/source/getting_started.md | 122 +++++++----------- 2 files changed, 49 insertions(+), 73 deletions(-) delete mode 100644 guides/assets/images/getting_started/forbidden_attributes_for_new_article.png diff --git a/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png b/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png deleted file mode 100644 index 50b178808ef0bd86c8a8f7022975a56a25ffa827..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9851 zcma)i1yEc|oAw|XBm==2+${umA3V4Rf-|_g1osf!2iE`r65Mrg2o@{^w-DUjmwRu0 zyWj5q_1B&{)#p9kb*i6!`jw}u&nH!7SqyYybN~Q=A@^2F9RNT?ezsRp5ue+$*E_xd z0KJ5ql(?oB!v2gn1^|GHOxbhY7y$5@>oF}vs-FUcCLsVg0T3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TPdg+Zw7EdL=qX#ao?H-hJ)Usir(#Wr9sL$ zC!7q)Z#esKB`j865~i{lzCmXyeDR{>@~}@=cr2@#kDZdfyueV8Msqb)7vvT-=R z&X-2h$}x5AR8->KrMCP5$E)?G1Iu`GajC=)0-~6FeDw4#BZaLV&b~8;)*T)Ee!wp@ zcsIvlYL2;rJZhS>aW~T1vrAJY8%N7wRIC&Nr$rH?2TRbIk+E~PO4qF>%AJl5yWf!y zA_7B~+U4a5xwY}OiROazAxC$2L-Ly{quSYFAi#j{#Ul&56Gk}bIS+Z6TYGx)vF-fk zGJK}D%f<2CI}3vNbn!#-Mz~JnsskL*8(nnY;%w=W5j!g+N&1yzCe8ff^2)?f4TUJDAI{!v1tvrc`|b7jEsZ7%y> z{T?UJUaxUXxjR-Ce$DmpAl@58LWoJ`&6qBp+W7=g#w1U2!Wo{`pkcjRfAO-nNlZxS z_|$ynuucvwX)WYnZ?z0H&`z~+y?G8oO#={;9)oqm@^Py=ySkxGQ=e~^z1YfMD!;f zk|9MKvMwzBAH>C#M-zwQBT;)vY4>|eB%7NHIF;GUITZ@D^*=ER!4XF!b5g65VVP{*o9dZLwvI*prTiv4O>ne^(A1O;? z*fm72l*|K;Qdv+TRTke~wuVWmsDvhq$zihlkZ~uvyYdh*ddhlzhC)hYL)X+bbNsS) zDfqtXN^1oDj?0+58y#g`g6=8u5V21^txHdv5KFy4SU4iG>fHTm1_|)lYjIEf2-@$C z585Z)zT{5m`9}At~Kr6ABTO` zFc{K~*3LFgrUECNgfj>clWVED&!za5>Px9_zi!QM0$OH%4Y;W?`Vxk z6CV_WfKVTi@pRpoEhJ-t1>E5Gep>kA#?{o+JK85bn8OWYzv)tcf{+h66fkGfM*Ej!NB~D5Q<1v-2>AI(XUo$) z_7gy2;8~X*wSob?G2%yl8O6Hgf0 you'll *almost* be able -to create an article. Try it! You should get an error that looks like this: - -![Forbidden attributes for new article] -(images/getting_started/forbidden_attributes_for_new_article.png) - -Rails has several security features that help you write secure applications, -and you're running into one of them now. This one is called [strong parameters](action_controller_overview.html#strong-parameters), -which requires us to tell Rails exactly which parameters are allowed into our -controller actions. - -Why do you have to bother? The ability to grab and automatically assign all -controller parameters to your model in one shot makes the programmer's job -easier, but this convenience also allows malicious use. What if a request to -the server was crafted to look like a new article form submit but also included -extra fields with values that violated your application's integrity? They would -be 'mass assigned' into your model and then into the database along with the -good stuff - potentially breaking your application or worse. - -We have to define our permitted controller parameters to prevent wrongful mass -assignment. In this case, we want to both allow and require the `title` and -`text` parameters for valid use of `create`. The syntax for this introduces -`require` and `permit`. The change will involve one line in the `create` -action: - -```ruby - @article = Article.new(params.require(:article).permit(:title, :text)) -``` - -This is often factored out into its own method so it can be reused by multiple -actions in the same controller, for example `create` and `update`. Above and -beyond mass assignment issues, the method is often made `private` to make sure -it can't be called outside its intended context. Here is the result: - -```ruby -def create - @article = Article.new(article_params) - - @article.save - redirect_to @article -end - -private - def article_params - params.require(:article).permit(:title, :text) +class ArticlesController < ApplicationController + def index + @articles = Article.all end + + def show + @article = Article.find(params[:id]) + end + + def new + @article = Article.new + end + + def create + @article = Article.new(article_params) + + if @article.save + redirect_to @article + else + render :new + end + end + + private + def article_params + params.require(:article).permit(:title, :body) + end +end ``` -TIP: For more information, refer to the reference above and -[this blog article about Strong Parameters] -(https://weblog.rubyonrails.org/2012/3/21/strong-parameters/). +TIP: To learn more about Strong Parameters, see [Action Controller Overview § +Strong Parameters](action_controller_overview.html#strong-parameters). ### Adding Some Validation From fdca84c63f21830df1220d44ad32eb40c9a4bbb3 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:00:37 -0500 Subject: [PATCH 15/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Validations?= =?UTF-8?q?=20and=20Error=20Messages=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38328. Co-Authored-By: Ryan Bigg Co-Authored-By: Tasos Latsas --- .../getting_started/form_with_errors.png | Bin 11665 -> 0 bytes guides/source/getting_started.md | 179 +++++++----------- 2 files changed, 71 insertions(+), 108 deletions(-) delete mode 100644 guides/assets/images/getting_started/form_with_errors.png diff --git a/guides/assets/images/getting_started/form_with_errors.png b/guides/assets/images/getting_started/form_with_errors.png deleted file mode 100644 index 6eefd2885aa4e5d78967b9b2d7b5eb8ab2469e6a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 11665 zcma)i1yr0(v*ti>_uvjgf&@=+8#G~%5Zo;UcXxM(V1pCfJp@f~_uwAf-S^G+-~aBt zXZN1nIdi7w%-d64UDeh7l!Pflr7_V+(Lf*&rmPG^83ck~0ls&kzyqI@?R?K55UaW@ zL`=mM?qG%k7sQUzA&Rhp{8q@5qmGE5^Fa?n553N$myKVyh6k-eK_Hj^H(5meXXO7x zdtoVhuen7=AbI=h10%k-&)323?ei^rK5w@i#n3=Y-a%3s#so)+#sw1)$QL{tws6Rp zGr4>649Qc%8`$D(Z`ZAqhVZ5zI`W^^7M1XmV|}hA@tJpSDZnmMW!^&of!fqyuC2@G zx1Q}#qyV_~c6AmhwPDkQ`mfMJ9k#FZvXI1ndsxq8l?F{XU!XdJ@hG5 z5GWfOs9fyW4wcG_!xH9WAwt(d3=o^qrN*Jo88OHP)DMfk9|-@2Imz zYkSDCkWud+GkLPN`2pQbh4mY2vJl10^qbe0sJsq?(2`^IZk!rcH)*ro>a{FEaeFp6 zR_f<0z0uEaGur!3UAfbTeSWvR2$joa!srN5mXr0(RdHts%svB&$A>DTFo-M1`HG5` zK@eyJbC}Tls^VDk;R}is(NwZ8@IL#z{k-VB9GPmTW-DA}Q#Cq-b-m&-A{4_iJUmRk zH+FV<>c<=t6VoKN$Y|{hygq+M_F>f$77@W_bARsvKadC0!74Gfry>DLi;Nuj0 z{=gW(`?a|E75Pv9Y;(c#IdWKffd)CT?nNbxMG)|1+7&4@dv*Azl6G=;+-1gu%hVyE}KZndN2c zw>2uNs`2Af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c-QVHipy5I=^kvdwI!&-79rF!!KJ0 zFR=0Js{{{1hJGEiE13Y{l$(|%x6*DA=)O0N48~gOoc?vkAODg6B})=VyGK9%&$wYBEl3#9Ux=%-U2o? zJ$>t1Md4fmnu|s#BtZ!aCUQXvCCMTDLAIFJ*!VUs=dQx{n}!AMU-4E}6a@4Ba&-WbU1hng|xtt5LiF(e@ zQJX?aOo`EZ5z5NSjMGh;OLM-=XhLF`H|tX>TSr14#L$?$Bb>w^WgHx8ZALQ}tYOA| z3EoG-vNP%%Jsf}g+$t+8MXI2rq@+8P?i2@r`u@|s+R!`|=XFOZ|8Bgs1Sw~6DdU*(?+$0+t8)oP&PPuA|THwFC zFE5X7d3t%7a+Y2_HruEXTC*-|R#Z~LFC?^ecX$|8lH1cGweeNl9gPxxPfvYubX1{@ z^K45Nz5axdO+C?3IU3zaam}z1>Zm6V7@hSk_otpu*{|Y!ljLAd4ow3@wdj5cg$Lfjt7nN1tC-HbW+gIN#sNW|G}Q83&x6yn& zBkqp{;`b2|=u2<4GB;hZFPO&37gYq4^Pw_MrejSlE$Pu9ESj}|cM(CA?C4BQ3{i>g zKT0nzod8bv<|W(;=FK_9oE7kMO*eF2HMTvWp`kOV;_O-2@!_g=2X(4LfzMGv7|zrO zW1kZf2t1`Mr{1+JS)h0|nsG}(L+aunc@?ue>4z`J(O;-h{MaL% znFH_`Tsz_X6$5CP80kH>w#XOhkBe{8Zjqhh@fJO_ED93qXVUQxT0M?O&pp3diNQq8 zaXEVWJpO-4>|q149~c<;6$ztDmHk^iB; and -try to save an article without a title, Rails will send you back to the -form, but that's not very useful. You need to tell the user that -something went wrong. To do that, you'll modify -`app/views/articles/new.html.erb` to check for error messages: +With our validations in place, let's modify `app/views/articles/new.html.erb` to +display any error messages for `title` and `body`: ```html+erb -<%= form_with scope: :article, url: articles_path, local: true do |form| %> +

New Article

- <% if @article.errors.any? %> -
-

- <%= pluralize(@article.errors.count, "error") %> prohibited - this article from being saved: -

-
    - <% @article.errors.full_messages.each do |msg| %> -
  • <%= msg %>
  • - <% end %> -
-
- <% end %> - -

+<%= form_with model: @article, local: true do |form| %> +

<%= form.label :title %>
<%= form.text_field :title %> -

+ <%= @article.errors.full_messages_for(:title).each do |message| %> +
<%= message %>
+ <% end %> +
-

- <%= form.label :text %>
- <%= form.text_area :text %> -

+
+ <%= form.label :body %>
+ <%= form.text_area :body %>
+ <%= @article.errors.full_messages_for(:body).each do |message| %> +
<%= message %>
+ <% end %> +
-

+

<%= form.submit %> -

- +
<% end %> - -<%= link_to 'Back', articles_path %> ``` -A few things are going on. We check if there are any errors with -`@article.errors.any?`, and in that case we show a list of all -errors with `@article.errors.full_messages`. +The [`full_messages_for`](https://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-full_messages_for) +method returns an array of user-friendly error messages for a specified +attribute. If there are no errors for that attribute, the array will be empty. -`pluralize` is a rails helper that takes a number and a string as its -arguments. If the number is greater than one, the string will be automatically -pluralized. +To understand how all of this works together, let's take another look at the +`new` and `create` controller actions: -The reason why we added `@article = Article.new` in the `ArticlesController` is -that otherwise `@article` would be `nil` in our view, and calling -`@article.errors.any?` would throw an error. +```ruby + def new + @article = Article.new + end -TIP: Rails automatically wraps fields that contain an error with a div -with class `field_with_errors`. You can define a CSS rule to make them -standout. + def create + @article = Article.new(article_params) -Now you'll get a nice error message when saving an article without a title when -you attempt to do that on the new article form -: + if @article.save + redirect_to @article + else + render :new + end + end +``` -![Form With Errors](images/getting_started/form_with_errors.png) +When we visit , the `GET /articles/new` +request is mapped to the `new` action. The `new` action does not attempt to save +`@article`. Therefore, validations are not checked, and there will be no error +messages. + +When we submit the form, the `POST /articles` request is mapped to the `create` +action. The `create` action *does* attempt to save `@article`. Therefore, +validations *are* checked. If any validation fails, `@article` will not be +saved, and `app/views/articles/new.html.erb` will be rendered with error +messages. + +TIP: To learn more about validations, see [Active Record Validations]( +active_record_validations.html). To learn more about validation error messages, +see [Active Record Validations § Working with Validation Errors]( +active_record_validations.html#working-with-validation-errors). ### Updating Articles From 9de4d98c60785ca9ad45023185ad675ad1c1d445 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:01:45 -0500 Subject: [PATCH 16/21] =?UTF-8?q?Add=20GS=20guide=20=C2=A7=20Finishing=20U?= =?UTF-8?q?p=20(Creating)=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38328. Co-Authored-By: Ryan Bigg --- guides/source/getting_started.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 7fe9238fcc..cd3bd32e72 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1042,6 +1042,26 @@ active_record_validations.html). To learn more about validation error messages, see [Active Record Validations § Working with Validation Errors]( active_record_validations.html#working-with-validation-errors). +#### Finishing Up + +We can now create an article by visiting . +To finish up, let's link to that page from the bottom of +`app/views/articles/index.html.erb`: + +```html+erb +

Articles

+ +
    + <% @articles.each do |article| %> +
  • + <%= link_to article.title, article %> +
  • + <% end %> +
+ +<%= link_to "New Article", new_article_path %> +``` + ### Updating Articles We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating From 0bf055e5f1955918fb8cf2abaa8276bd70fb9643 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:03:17 -0500 Subject: [PATCH 17/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Updating=20a?= =?UTF-8?q?n=20Article=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38329. Co-Authored-By: Ryan Bigg Co-Authored-By: Petrik de Heus --- .../index_action_with_edit_link.png | Bin 9703 -> 0 bytes guides/source/getting_started.md | 213 +++++------------- 2 files changed, 54 insertions(+), 159 deletions(-) delete mode 100644 guides/assets/images/getting_started/index_action_with_edit_link.png diff --git a/guides/assets/images/getting_started/index_action_with_edit_link.png b/guides/assets/images/getting_started/index_action_with_edit_link.png deleted file mode 100644 index a2a087a5989405a062541a90931ca310bdceb2a1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9703 zcmbW7Wn5HU*YH75P)b5V8l)QpB!*BzI)_dvr8|ZaX%G;QE(Jsw8l)LI1%`4+=^i?U zj^}t?*L}bD`@HX$=Q$ttZ_b&0*4cZlz1Lprzjvg%syyDKr;pIk(C`#q%V?sZp??Q{ zFJRvT%DL$1Ni;O3G6flFZEy6QUwr6obZ8hA56g0*cKMVK;v@RbFq*p{?@?$aSY{x! zZFD_?HVE1TMg`4#6gS5I`|STz`%~lJcTR@0&%$BVXt9*5F6QZzHuLAj?efui zj&qH1Ol=*ZI5pU}b=v!V@r+rxWyVd(o8znSEU{%$l?)-&-sQbvDA8WGMyWwf1ZJwR zQ#NV)T{4o(xKTUmuH|a4$?w8-8Sb*&6^R{8X6iSZVJ8-NdtGHUQ0ufnW zk)?W-Z*=^qB>bk<2tD`b=U~2PkDjoTu&TembC~&|UtTvg*F$$W*3ZU7(Q29Qx?VpT+_Ib>FQx2c0tsSp7UfO`3jJ&1$qo zK({B$S*YKcd2x&AmKlMe+7BJ$SJXBEoeZT5DkXF3w+8sDhK#qFU&%g5rp=ih%ae}- zG^xprDAuKw3}`kgI_q9~9K`FqC^Z=IghOYQxq8rQUIHNoIbrom=vdBnwyBr2q&rZg zF(^bT>{bfA9if{Lbz5u;YQ4Li{b7l-4;U<8A<=#|N5WsUgF0jplc_jE*eQ8P;`ZEv z|N3gbb=EWRVl8E3IEQwB&&9=s6^=n5ZZ;dL#BI`i?luhStzx~;K=ir8q}lt>Yr&sv zsq^M+Ax<+#+=qW2HkkQE?3-fJ(U4F;7@tLtw-FV->??geI5BZmmVk|_Hgi4H3;B!9 zGB)@s8^0aqefVZE$>?DqVz$B@146j;dZ{Bc(lp697U(J#ct1(k#AiLt#{Mcw)0xuY zYQEV=t7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-$PY)1&ELpE_BEWd5o1A7E*e;5GEbK{!F5*_= zRu(@7a-5g8x3^szy7ICCQ!M)r-ibz7NWB1;XP~4C5Z7dc0&I)BY>S|ZV%Ta#D;$4A zb@fk2nTWH3*zcCDZtI`S-#pyDt&>#Dd%h*<*jo46$iCJUTINqs!j(XBkpKR>CYMb8 z(k3n0j*zo!bZJ=rdzSm+Nl1Mv<5e_9Vw?3}oY2#K=k-aYZ<_Ygm2b~)HriTeiY(B9 zV^r^@Zzrm!z>m%U+YzaM9f11#z|_C){^JbQUyuHMqU!%&;a}C~@&2hORe91`yvt!c R1$-GzK~`0!?4{X<{{qAUWUc@J diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index cd3bd32e72..b5ff13e9db 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1062,180 +1062,75 @@ To finish up, let's link to that page from the bottom of <%= link_to "New Article", new_article_path %> ``` -### Updating Articles +### Updating an Article -We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating -articles. +We've covered the "CR" of CRUD. Now let's move on to the "U" (Update). Updating +a resource is very similar to creating a resource. They are both multi-step +processes. First, the user requests a form to edit the data. Then, the user +submits the form. If there are no errors, then the resource is updated. Else, +the form is redisplayed with error messages, and the process is repeated. -The first step we'll take is adding an `edit` action to the -`ArticlesController`, generally between the `new` and `create` -actions, as shown: +These steps are conventionally handled by a controller's `edit` and `update` +actions. Let's add a typical implementation of these actions to +`app/controllers/articles_controller.rb`, below the `create` action: ```ruby -def new - @article = Article.new -end - -def edit - @article = Article.find(params[:id]) -end - -def create - @article = Article.new(article_params) - - if @article.save - redirect_to @article - else - render 'new' +class ArticlesController < ApplicationController + def index + @articles = Article.all end -end -``` -NOTE: We're using `edit` to render a view. For the actual -saving of the changes to the Article, we'll add an `update` action later. - - -The view will contain a form similar to the one we used when creating -new articles. Create a file called `app/views/articles/edit.html.erb` and make -it look as follows: - -```html+erb -

Edit Article

- -<%= form_with model: @article, local: true do |form| %> - - <% if @article.errors.any? %> -
-

- <%= pluralize(@article.errors.count, "error") %> prohibited - this article from being saved: -

-
    - <% @article.errors.full_messages.each do |msg| %> -
  • <%= msg %>
  • - <% end %> -
-
- <% end %> - -

- <%= form.label :title %>
- <%= form.text_field :title %> -

- -

- <%= form.label :text %>
- <%= form.text_area :text %> -

- -

- <%= form.submit %> -

- -<% end %> - -<%= link_to 'Back', articles_path %> -``` - -This time we point the form to the `update` action, which is not defined yet -but will be very soon. - -Passing the article object to the `form_with` method will automatically set the URL for -submitting the edited article form. This option tells Rails that we want this -form to be submitted via the `PATCH` HTTP method, which is the HTTP method you're -expected to use to **update** resources according to the REST protocol. - -Also, passing a model object to `form_with`, like `model: @article` in the edit -view above, will cause form helpers to fill in form fields with the corresponding -values of the object. Passing in a symbol scope such as `scope: :article`, as -was done in the new view, only creates empty form fields. -More details can be found in [form_with documentation] -(https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with). - -Next, we need to create the `update` action in -`app/controllers/articles_controller.rb`. -Add it between the `create` action and the `private` method: - -```ruby -def edit - @article = Article.find(params[:id]) -end - -def create - @article = Article.new(article_params) - - if @article.save - redirect_to @article - else - render 'new' + def show + @article = Article.find(params[:id]) end -end -def update - @article = Article.find(params[:id]) - - if @article.update(article_params) - redirect_to @article - else - render 'edit' + def new + @article = Article.new end -end -private - def article_params - params.require(:article).permit(:title, :text) + def create + @article = Article.new(article_params) + + if @article.save + redirect_to @article + else + render :new + end end + + def edit + @article = Article.find(params[:id]) + end + + def update + @article = Article.find(params[:id]) + + if @article.update(article_params) + redirect_to @article + else + render :edit + end + end + + private + def article_params + params.require(:article).permit(:title, :body) + end +end ``` -The new method, `update`, is used when you want to update a record -that already exists, and it accepts a hash containing the attributes -that you want to update. As before, if there was an error updating the -article we want to show the form back to the user. +Notice how the `edit` and `update` actions resemble the `new` and `create` +actions. -We reuse the `article_params` method that we defined earlier for the create -action. +The `edit` action fetches the article from the database, and stores it in +`@article` so that it can be used when building the form. By default, the `edit` +action will render `app/views/articles/edit.html.erb`. -TIP: It is not necessary to pass all the attributes to `update`. For example, -if `@article.update(title: 'A new title')` was called, Rails would only update -the `title` attribute, leaving all other attributes untouched. - -Finally, we want to show a link to the `edit` action in the list of all the -articles, so let's add that now to `app/views/articles/index.html.erb` to make -it appear next to the "Show" link: - -```html+erb -
- - - - - - - <% @articles.each do |article| %> - - - - - - - <% end %> -
TitleText
<%= article.title %><%= article.text %><%= link_to 'Show', article_path(article) %><%= link_to 'Edit', edit_article_path(article) %>
-``` - -And we'll also add one to the `app/views/articles/show.html.erb` template as -well, so that there's also an "Edit" link on an article's page. Add this at the -bottom of the template: - -```html+erb -... - -<%= link_to 'Edit', edit_article_path(@article) %> | -<%= link_to 'Back', articles_path %> -``` - -And here's how our app looks so far: - -![Index action with edit link](images/getting_started/index_action_with_edit_link.png) +The `update` action (re-)fetches the article from the database, and attempts +to update it with the submitted form data filtered by `article_params`. If no +validations fail and the update is successful, the action redirects the browser +to the article's page. Else, the action redisplays the form, with error +messages, by rendering `app/views/articles/edit.html.erb`. ### Using partials to clean up duplication in views From e7b6928704e8fc74bbf159669af73e9284113fb1 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:04:03 -0500 Subject: [PATCH 18/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Using=20Part?= =?UTF-8?q?ials=20to=20Share=20View=20Code=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38329. Co-Authored-By: Ryan Bigg --- guides/source/getting_started.md | 91 ++++++++++++++------------------ 1 file changed, 40 insertions(+), 51 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index b5ff13e9db..620d27aacb 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1132,82 +1132,71 @@ validations fail and the update is successful, the action redirects the browser to the article's page. Else, the action redisplays the form, with error messages, by rendering `app/views/articles/edit.html.erb`. -### Using partials to clean up duplication in views +#### Using Partials to Share View Code -Our `edit` page looks very similar to the `new` page; in fact, they -both share the same code for displaying the form. Let's remove this -duplication by using a view partial. By convention, partial files are -prefixed with an underscore. +Our `edit` form will look the same as our `new` form. Even the code will be the +same, thanks to the Rails form builder and resourceful routing. The form builder +automatically configures the form to make the appropriate kind of request, based +on whether the model object has been previously saved. -TIP: You can read more about partials in the -[Layouts and Rendering in Rails](layouts_and_rendering.html) guide. - -Create a new file `app/views/articles/_form.html.erb` with the following -content: +Because the code will be the same, we're going to factor it out into a shared +view called a *partial*. Let's create `app/views/articles/_form.html.erb` with +the following contents: ```html+erb -<%= form_with model: @article, local: true do |form| %> - - <% if @article.errors.any? %> -
-

- <%= pluralize(@article.errors.count, "error") %> prohibited - this article from being saved: -

-
    - <% @article.errors.full_messages.each do |msg| %> -
  • <%= msg %>
  • - <% end %> -
-
- <% end %> - -

+<%= form_with model: article, local: true do |form| %> +

<%= form.label :title %>
<%= form.text_field :title %> -

+ <%= article.errors.full_messages_for(:title).each do |message| %> +
<%= message %>
+ <% end %> +
-

- <%= form.label :text %>
- <%= form.text_area :text %> -

+
+ <%= form.label :body %>
+ <%= form.text_area :body %>
+ <%= article.errors.full_messages_for(:body).each do |message| %> +
<%= message %>
+ <% end %> +
-

+

<%= form.submit %> -

- +
<% end %> ``` -Everything except for the `form_with` declaration remained the same. -The reason we can use this shorter `form_with` declaration -to stand in for either of the other forms is that `@article` is a *resource* -corresponding to a full set of RESTful routes, and Rails is able to infer -which URI and method to use. -For more information about this use of `form_with`, see [Resource-oriented style] -(https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with-label-Resource-oriented+style). +The above code is the same as our form in `app/views/articles/new.html.erb`, +except that all occurrences of `@article` have been replaced with `article`. +Because partials are shared code, it is best practice that they do not depend on +specific instance variables set by a controller action. Instead, we will pass +the article to the partial as a local variable. -Now, let's update the `app/views/articles/new.html.erb` view to use this new -partial, rewriting it completely: +Let's update `app/views/articles/new.html.erb` to use the partial via [`render`]( +https://api.rubyonrails.org/classes/ActionView/Helpers/RenderingHelper.html#method-i-render): ```html+erb

New Article

-<%= render 'form' %> - -<%= link_to 'Back', articles_path %> +<%= render "form", article: @article %> ``` -Then do the same for the `app/views/articles/edit.html.erb` view: +NOTE: A partial's filename must be prefixed **with** an underscore, e.g. +`_form.html.erb`. But when rendering, it is referenced **without** the +underscore, e.g. `render "form"`. + +And now, let's create a very similar `app/views/articles/edit.html.erb`: ```html+erb

Edit Article

-<%= render 'form' %> - -<%= link_to 'Back', articles_path %> +<%= render "form", article: @article %> ``` +TIP: To learn more about partials, see [Layouts and Rendering in Rails § Using +Partials](layouts_and_rendering.html#using-partials). + ### Deleting Articles We're now ready to cover the "D" part of CRUD, deleting articles from the From 0d3f15a0221bc0765b6829513579aece88d6179e Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:04:25 -0500 Subject: [PATCH 19/21] =?UTF-8?q?Add=20GS=20guide=20=C2=A7=20Finishing=20U?= =?UTF-8?q?p=20(Updating)=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38329. Co-Authored-By: Ryan Bigg --- guides/source/getting_started.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 620d27aacb..84993eedbe 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1197,6 +1197,22 @@ And now, let's create a very similar `app/views/articles/edit.html.erb`: TIP: To learn more about partials, see [Layouts and Rendering in Rails § Using Partials](layouts_and_rendering.html#using-partials). +#### Finishing Up + +We can now update an article by visiting its edit page, e.g. +. To finish up, let's link to the edit +page from the bottom of `app/views/articles/show.html.erb`: + +```html+erb +

<%= @article.title %>

+ +

<%= @article.body %>

+ +
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
+``` + ### Deleting Articles We're now ready to cover the "D" part of CRUD, deleting articles from the From 8fa3b80ba2ac617b5b1292e5617dc5e487b7b56d Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:06:07 -0500 Subject: [PATCH 20/21] =?UTF-8?q?Edit=20GS=20guide=20=C2=A7=20Deleting=20a?= =?UTF-8?q?n=20Article=20[ci-skip]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #39920. Resolves #38329. Co-Authored-By: Ryan Bigg Co-Authored-By: Petrik de Heus --- .../images/getting_started/confirm_dialog.png | Bin 17507 -> 0 bytes guides/source/getting_started.md | 130 ++++++------------ 2 files changed, 45 insertions(+), 85 deletions(-) delete mode 100644 guides/assets/images/getting_started/confirm_dialog.png diff --git a/guides/assets/images/getting_started/confirm_dialog.png b/guides/assets/images/getting_started/confirm_dialog.png deleted file mode 100644 index ce65734e6c4e9f027af6e04fb05f954d431d6f0e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17507 zcmcJ$WmFtZ*ETwk00|yKfCLQ$2p)nHAQ0T$H8=zY3GM`U3lJD2xVsDt79c?I;4%yn zbZ~b$P44@7-fw+ty=SfSLyUhbbvY;$XhO1c5*}(o$k7AkYIL z5D3NT5h_q(d*WFN0*Nt7i;1YYp=?k3V*`b#mDdi`kvTgBZkV7SkDVWYlz2d(aU75^ z0`&jOPa~jTbZTY7kOK}hEJhg+=9f*BFZlQ`pX5J`r$wcTd&Q_C{D>+j%qg8-ug{FA zy>zC8y`dRwLc~7uao-&+J(#PGI9{$e$uTIo%d)UKPUK(i!m)h*wsyWoL+_IW9fpi zOU*ej^v1`RKb_2Nf(0@tA@(*Ul3k+g&H{H7&<5;Tgm@I=Bh=xepM9&2FE|#?jxMgZ zEGwoyZ>eK&Lgd|3hu5vWcgXrzN1+B4@nDc&j3QK}DA)J<81h%bx_d&en2AQ2Mpx>f zId01)$V84YF}^^jEWgbpS|;xU3qDvO zTlYr2Zvj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^(j001=hY; z+0Xa#rWvLcn1p-80-XUzzojBD#N3>mkDLm}g{ekIn_jBtXJ`1*>FMd!`PKS+A>u=- zM$WS}YaqUTiUmh#HMX_g6<{|W7o<44x}NRt@7vw?Yby#XL;Fvhmc0*M{|4?+Ahi7p z0UjVvRe-qse}`q=wgp2+01Z_0BTr31zd7aA)jd5ucdCU?lP1hOM|Z41Mw~AuoLicj z;H+4tcG~*&^oR^}5H2omaOaZI8_Q(0YG^4d95@-@AF#_`K4|dvw0Chi&92A;W??}N za6~1={DOkE3k0Sn0qsZ&OuHvmjLNTVd= z$FM^>VdW*on`c*)lZJNb-deue%JpBx%`~+&xlid3bO_LI`?ePJ^E+ce;J@G80gAo< wZtws21n+;zAozd(Zts7o^#ARXz-tTw`Km|czWj7R&OAt3TtTe-gVC4&2XBrSPXGV_ diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 84993eedbe..292a44a876 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1213,41 +1213,16 @@ page from the bottom of `app/views/articles/show.html.erb`: ``` -### Deleting Articles +### Deleting an Article -We're now ready to cover the "D" part of CRUD, deleting articles from the -database. Following the REST convention, the route for -deleting articles as per output of `bin/rails routes` is: +Finally, we arrive at the "D" (Delete) of CRUD. Deleting a resource is a simpler +process than creating or updating. It only requires a route and a controller +action. And our resourceful routing (`resources :articles`) already provides the +route, which maps `DELETE /articles/:id` requests to the `destroy` action of +`ArticlesController`. -```ruby -DELETE /articles/:id(.:format) articles#destroy -``` - -The `delete` routing method should be used for routes that destroy -resources. If this was left as a typical `get` route, it could be possible for -people to craft malicious URLs like this: - -```html -look at this cat! -``` - -We use the `delete` method for destroying resources, and this route is mapped -to the `destroy` action inside `app/controllers/articles_controller.rb`, which -doesn't exist yet. The `destroy` method is generally the last CRUD action in -the controller, and like the other public CRUD actions, it must be placed -before any `private` or `protected` methods. Let's add it: - -```ruby -def destroy - @article = Article.find(params[:id]) - @article.destroy - - redirect_to articles_path -end -``` - -The complete `ArticlesController` in the -`app/controllers/articles_controller.rb` file should now look like this: +So, let's add a typical `destroy` action to `app/controllers/articles_controller.rb`, +below the `update` action: ```ruby class ArticlesController < ApplicationController @@ -1263,27 +1238,27 @@ class ArticlesController < ApplicationController @article = Article.new end - def edit - @article = Article.find(params[:id]) - end - def create @article = Article.new(article_params) if @article.save redirect_to @article else - render 'new' + render :new end end + def edit + @article = Article.find(params[:id]) + end + def update @article = Article.find(params[:id]) if @article.update(article_params) redirect_to @article else - render 'edit' + render :edit end end @@ -1291,68 +1266,53 @@ class ArticlesController < ApplicationController @article = Article.find(params[:id]) @article.destroy - redirect_to articles_path + redirect_to root_path end private def article_params - params.require(:article).permit(:title, :text) + params.require(:article).permit(:title, :body) end end ``` -You can call `destroy` on Active Record objects when you want to delete -them from the database. Note that we don't need to add a view for this -action since we're redirecting to the `index` action. +The `destroy` action fetches the article from the database, and calls [`destroy`]( +https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-destroy) +on it. Then, it redirects the browser to the root path. -Finally, add a 'Destroy' link to your `index` action template -(`app/views/articles/index.html.erb`) to wrap everything together. +We have chosen to redirect to the root path because that is our main access +point for articles. But, in other circumstances, you might choose to redirect to +e.g. `articles_path`. + +Now let's add a link at the bottom of `app/views/articles/show.html.erb` so that +we can delete an article from its own page: ```html+erb -

Listing Articles

-<%= link_to 'New article', new_article_path %> - - - - - - +

<%= @article.title %>

- <% @articles.each do |article| %> - - - - - - - - <% end %> -
TitleText
<%= article.title %><%= article.text %><%= link_to 'Show', article_path(article) %><%= link_to 'Edit', edit_article_path(article) %><%= link_to 'Destroy', article_path(article), - method: :delete, - data: { confirm: 'Are you sure?' } %>
+

<%= @article.body %>

+ +
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
  • <%= link_to "Destroy", article_path(@article), + method: :delete, + data: { confirm: "Are you sure?" } %>
  • +
``` -Here we're using `link_to` in a different way. We pass the named route as the -second argument, and then the options as another argument. The `method: :delete` -and `data: { confirm: 'Are you sure?' }` options are used as HTML5 attributes so -that when the link is clicked, Rails will first show a confirm dialog to the -user, and then submit the link with method `delete`. This is done via the -JavaScript file `rails-ujs` which is automatically included in your -application's layout (`app/views/layouts/application.html.erb`) when you -generated the application. Without this file, the confirmation dialog box won't -appear. +In the above code, we're passing a few additional options to `link_to`. The +`method: :delete` option causes the link to make a `DELETE` request instead of a +`GET` request. The `data: { confirm: "Are you sure?" }` option causes a +confirmation dialog to appear when the link is clicked. If the user cancels the +dialog, the request is aborted. Both of these options are powered by a feature +of Rails called *Unobtrusive JavaScript* (UJS). The JavaScript file that +implements these behaviors is included by default in fresh Rails applications. -![Confirm Dialog](images/getting_started/confirm_dialog.png) +TIP: To learn more about Unobtrusive JavaScript, see [Working With JavaScript in +Rails](working_with_javascript_in_rails.html). -TIP: Learn more about Unobtrusive JavaScript on -[Working With JavaScript in Rails](working_with_javascript_in_rails.html) guide. - -Congratulations, you can now create, show, list, update, and destroy -articles. - -TIP: In general, Rails encourages using resources objects instead of -declaring routes manually. For more information about routing, see -[Rails Routing from the Outside In](routing.html). +And that's it! We can now list, show, create, update, and delete articles! +InCRUDable! Adding a Second Model --------------------- From 3beb8c0fe0eabd31fd5c7ea481820186244621e1 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Wed, 28 Oct 2020 17:07:09 -0500 Subject: [PATCH 21/21] Update GS guide code for Adding a Second Model [ci-skip] --- guides/source/getting_started.md | 167 +++++++++++++------------------ 1 file changed, 72 insertions(+), 95 deletions(-) diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 292a44a876..93239176b5 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1416,8 +1416,9 @@ association: ```ruby class Article < ApplicationRecord has_many :comments - validates :title, presence: true, - length: { minimum: 5 } + + validates :title, presence: true + validates :body, presence: true, length: { minimum: 10 } end ``` @@ -1436,8 +1437,12 @@ knows where we would like to navigate to see `comments`. Open up the `config/routes.rb` file again, and edit it as follows: ```ruby -resources :articles do - resources :comments +Rails.application.routes.draw do + root "articles#index" + + resources :articles do + resources :comments + end end ``` @@ -1477,15 +1482,16 @@ So first, we'll wire up the Article show template (`app/views/articles/show.html.erb`) to let us make a new comment: ```html+erb -

- Title: - <%= @article.title %> -

+

<%= @article.title %>

-

- Text: - <%= @article.text %> -

+

<%= @article.body %>

+ +
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
  • <%= link_to "Destroy", article_path(@article), + method: :delete, + data: { confirm: "Are you sure?" } %>
  • +

Add a comment:

<%= form_with model: [ @article, @article.comments.build ], local: true do |form| %> @@ -1501,9 +1507,6 @@ So first, we'll wire up the Article show template <%= form.submit %>

<% end %> - -<%= link_to 'Edit', edit_article_path(@article) %> | -<%= link_to 'Back', articles_path %> ``` This adds a form on the `Article` show page that creates a new comment by @@ -1545,15 +1548,16 @@ the `show` action of the `ArticlesController` which in turn renders the add that to the `app/views/articles/show.html.erb`. ```html+erb -

- Title: - <%= @article.title %> -

+

<%= @article.title %>

-

- Text: - <%= @article.text %> -

+

<%= @article.body %>

+ +
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
  • <%= link_to "Destroy", article_path(@article), + method: :delete, + data: { confirm: "Are you sure?" } %>
  • +

Comments

<% @article.comments.each do |comment| %> @@ -1582,9 +1586,6 @@ add that to the `app/views/articles/show.html.erb`. <%= form.submit %>

<% end %> - -<%= link_to 'Edit', edit_article_path(@article) %> | -<%= link_to 'Back', articles_path %> ``` Now you can add articles and comments to your blog and have them show up in the @@ -1621,15 +1622,16 @@ Then you can change `app/views/articles/show.html.erb` to look like the following: ```html+erb -

- Title: - <%= @article.title %> -

+

<%= @article.title %>

-

- Text: - <%= @article.text %> -

+

<%= @article.body %>

+ +
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
  • <%= link_to "Destroy", article_path(@article), + method: :delete, + data: { confirm: "Are you sure?" } %>
  • +

Comments

<%= render @article.comments %> @@ -1648,9 +1650,6 @@ following: <%= form.submit %>

<% end %> - -<%= link_to 'Edit', edit_article_path(@article) %> | -<%= link_to 'Back', articles_path %> ``` This will now render the partial in `app/views/comments/_comment.html.erb` once @@ -1683,24 +1682,22 @@ create a file `app/views/comments/_form.html.erb` containing: Then you make the `app/views/articles/show.html.erb` look like the following: ```html+erb -

- Title: - <%= @article.title %> -

+

<%= @article.title %>

-

- Text: - <%= @article.text %> -

+

<%= @article.body %>

+ +
    +
  • <%= link_to "Edit", edit_article_path(@article) %>
  • +
  • <%= link_to "Destroy", article_path(@article), + method: :delete, + data: { confirm: "Are you sure?" } %>
  • +

Comments

<%= render @article.comments %>

Add a comment:

<%= render 'comments/form' %> - -<%= link_to 'Edit', edit_article_path(@article) %> | -<%= link_to 'Back', articles_path %> ``` The second render just defines the partial template we want to render, @@ -1730,8 +1727,9 @@ Within the `article` model, after running a migration to add a `status` column, ```ruby class Article < ApplicationRecord has_many :comments - validates :title, presence: true, - length: { minimum: 5 } + + validates :title, presence: true + validates :body, presence: true, length: { minimum: 10 } VALID_STATUSES = ['public', 'private', 'archived'] @@ -1762,29 +1760,19 @@ end Then, in our `index` action template (`app/views/articles/index.html.erb`) we would use the `archived?` method to avoid displaying any article that is archived: ```html+erb -

Listing Articles

-<%= link_to 'New article', new_article_path %> - - - - - - +

Articles

+
    <% @articles.each do |article| %> <% unless article.archived? %> -
- - - - - - +
  • + <%= link_to article.title, article %> +
  • <% end %> <% end %> -
    TitleText
    <%= article.title %><%= article.text %><%= link_to 'Show', article_path(article) %><%= link_to 'Edit', edit_article_path(article) %><%= link_to 'Destroy', article_path(article), - method: :delete, - data: { confirm: 'Are you sure?' } %>
    + + +<%= link_to "New Article", new_article_path %> ``` However, if you look again at our models now, you can see that the logic is duplicated. If in the future we increase the functionality of our blog - to include private messages, for instance - we might find ourselves duplicating the logic yet again. This is where concerns come in handy. @@ -1829,9 +1817,8 @@ class Article < ApplicationRecord include Visible has_many :comments - validates :title, presence: true, - length: { minimum: 5 } - + validates :title, presence: true + validates :body, presence: true, length: { minimum: 10 } end ``` @@ -1871,30 +1858,19 @@ end Then in the view, you can call it like any class method: ```html+erb -

    Listing Articles

    -Our blog has <%= Article.public_count %> articles and counting! Add yours now. -<%= link_to 'New article', new_article_path %> - - - - - - +

    Articles

    +Our blog has <%= Article.public_count %> articles and counting! + +
      <% @articles.each do |article| %> - <% unless article.archived? %> -
    - - - - - - - <% end %> +
  • + <%= link_to article.title, article %> +
  • <% end %> -
    TitleText
    <%= article.title %><%= article.text %><%= link_to 'Show', article_path(article) %><%= link_to 'Edit', edit_article_path(article) %><%= link_to 'Destroy', article_path(article), - method: :delete, - data: { confirm: 'Are you sure?' } %>
    + + +<%= link_to "New Article", new_article_path %> ``` Deleting Comments @@ -1921,7 +1897,7 @@ So first, let's add the delete link in the

    <%= link_to 'Destroy Comment', [comment.article, comment], method: :delete, - data: { confirm: 'Are you sure?' } %> + data: { confirm: "Are you sure?" } %>

    ``` @@ -1969,8 +1945,9 @@ class Article < ApplicationRecord include Visible has_many :comments, dependent: :destroy - validates :title, presence: true, - length: { minimum: 5 } + + validates :title, presence: true + validates :body, presence: true, length: { minimum: 10 } end ```