Open the config/routes.rb
file and add a new route to it.
Rails.application.routes.draw do
resources :habits, only: [:show]
end
This route instructs Rails to call the show
action method on the HabitsController
class, when a user visits the path /habits/1
. You can also access this route programmatically using the URL helper named habit_path
, passing an instance of Habit
model.
If you want to learn routing in detail, check out the following article: Understanding the Rails Router: Why, What, and How
Now that we have a valid route to show the habit, we need a controller and action to handle the incoming request.