Learn Ruby on Rails Book

Adding Tasks controller

Controller and Actions

Rails Controllers are Ruby classes, that store a series of actions. Let's generate a controller to carry out tasks related operations.

1$ bundle exec rails generate controller Tasks
2Running via Spring preloader in process 28301
3      create  app/controllers/tasks_controller.rb
4      invoke  erb
5      create    app/views/tasks
6      invoke  test_unit
7      create    test/controllers/tasks_controller_test.rb
8      invoke  helper
9      create    app/helpers/tasks_helper.rb
10      invoke    test_unit
11      invoke  assets
12      invoke    coffee
13      create      app/assets/javascripts/tasks.coffee
14      invoke    scss
15      create      app/assets/stylesheets/tasks.scss

Notice that the above command created multiple files. Let's open the controller file app/controllers/tasks_controller.rb and add action index to TasksController.

1class TasksController < ApplicationController
2  def index
3    render html: "This is index action of Tasks controller"
4  end
5end

Now go to the browser and hit http://localhost:3000/tasks.

We will get a routing error.

1Routing Error
2No route matches [GET] "/tasks"

Defining routes to map requests to controller actions

Whenever we hit URL on the browser, Rails looks up for that path in the routes file. If a match is found, then the mapped action in the controller is invoked.

We want to route all requests to route URL /tasks to index action of TasksController. We will be using the resources routing.

Open config/routes.rb and add following lines.

1Rails.application.routes.draw do
2  resources :tasks, only: :index
3end

Now visit http://localhost:3000/tasks and this time we should get message This is index action of Tasks controller.

Adding View file

In the above case the content was rendered by the controller itself. We want views to deliver the final html content.

So let's create file /app/views/tasks/index.html.erb.

1touch app/views/tasks/index.html.erb

Open /app/views/tasks/index.html.erb file and add following line.

1<H1>List of tasks will appear here</H1>

Open /app/controllers/tasks_controller.rb then change the action index.

1class TasksController < ApplicationController
2  def index
3    render
4  end
5end

Now visit http://localhost:3000/tasks and this time we should get message List of tasks will appear here.

Using Rails console to add tasks

Now open your terminal and type bundle exec rails console.

1$ bundle exec rails console
2Running via Spring preloader in process 10511
3Loading development environment (Rails 5.2.2)
4irb(main):001:0>

Let's create a Task record and save it in the table.

1irb(main):005:0> task = Task.new(title:"This is my first task")
2=> #<Task id: nil, title: "This is my first task", created_at: nil, updated_at: nil>
3
4irb(main):006:0> task.save
5   (0.3ms)  begin transaction
6  Task Create (0.7ms)  INSERT INTO "tasks" ("title", "created_at", "updated_at") VALUES (?, ?, ?)  [["title", "This is my first task"], ["created_at", "2019-01-30 07:35:50.117114"], ["updated_at", "2019-01-30 07:35:50.117114"]]
7   (196.3ms)  commit transaction

Let's create one more task and try to save this record.

1irb(main):007:0> task = Task.new(title:"This is my second task")
2=> #<Task id: nil, title: "This is my second task", created_at: nil, updated_at: nil>
3
4irb(main):008:0> task.save
5   (0.1ms)  begin transaction
6  Task Create (0.6ms)  INSERT INTO "tasks" ("title", "created_at", "updated_at") VALUES (?, ?, ?)  [["title", "This is my second task"], ["created_at", "2019-01-30 07:36:01.248102"], ["updated_at", "2019-01-30 07:36:01.248102"]]
7   (181.4ms)  commit transaction

Calling task.save saves the object in the database. A row is added in tasks table.

Now we can see all the tasks stored in the database using Task.all.

1irb(main):009:0> Task.all
2  Task Load (0.3ms)  SELECT  "tasks".* FROM "tasks" LIMIT ?  [["LIMIT", 11]]
3=> #<ActiveRecord::Relation [#<Task id: 3, title: "This is my first task", created_at: "2019-01-30 07:35:50", updated_at: "2019-01-30 07:35:50">, #<Task id: 4, title: "This is my second task", created_at: "2019-01-30 07:36:01", updated_at: "2019-01-30 07:36:01">]>

We will be learning how to display all these tasks in the coming chapters.

Now let's commit these changes:

1git add -A
2git commit -m "Added tasks controller and index action"