Let's change the routes.rb
file to change the home page to the tasks
page instead of the Rails welcome page. For this, open the routes.rb
file and add the following directive at the top.
Rails.application.routes.draw do
root "tasks#index"
resources :tasks
end
Rails recommends putting the root route at the top of config/routes.rb
, as it will be matched first, since a home page is the most popular route of most Rails applications.
Now restart your Rails app, and reload the browser. You should see an error.
Rails throws this error because we haven’t created our index
action yet. Let's create that now.
In addition to the migration, the rails generate resource
command should have also created an empty TasksController
for you.
class TasksController < ApplicationController
end
Let’s create our first action called index
to display all the tasks. In this action, we will fetch all the tasks from the database.
class TasksController < ApplicationController
def index
@tasks = Task.all
end
end
Creating action is not enough. If you reload the page, you should see a different error because we didn’t create the template (view) corresponding to this action.
Let’s fix that. In the app/views/tasks
directory, add a file named index.html.erb
.
<h1 class="font-bold text-2xl">Task Manager</h1>
If you reload the browser now, the words “Task Manager” should greet you. If you see the following view, that means Tailwind is working correctly, too.