In this challenge, you will meet your old friend Active Record, which is Rails' ORM.
Build a Rails todolist application. Your todo-app should have 7 entry points in the routing:
GET '/tasks'
: get all your tasks.GET '/tasks/:id'
: get a precise task, e.gGET '/tasks/33'
get task with id=3GET '/tasks/new'
: get the form to create a new taskPOST '/tasks'
: post a new taskGET '/tasks/:id/edit'
: get the form to edit an existing taskPATCH '/tasks/:id'
: update an existing taskDELETE '/tasks/:id'
: delete an existing task
You will have to create a TasksController
with 7 actions related to those 7 routes. For the names of these actions, use Rails naming convention.
- index
- show
- new
- create
- edit
- update
- destroy
Before starting to build your routes, your controller and views, generate your model:
- Use
rails generate model <ModelName> <attr1>:<type> <attr2>:<type> ..
to create the model and associated migration all at the same time. - If you forget a field in your model, you can use
rails generate migration <MigrationName>
to automatically create a new migration file with good timestamps. - You still have to run the good
rake db:migrate
to execute your migrations. - Once that's done, play with the Rails console. This is a IRB-on-steroids that enables you to interact with your Rails application from the command line. You can try to add new tasks to your DB directly from the command line.
Some guidelines to build your views
index.html.erb
- Should display a list of all tasks and, for each task:
- a link to its show view (use a
link_to
helper) - a link to its edit view
- a link to its delete action. Tips: a standard link does not allow to perform
DELETE
request, so here you should add amethod: :delete
option to yourlink_to
helper.
- a link to its show view (use a
- Should include a link to the new view to create a new task
show.html.erb
- Should display the task's details (content, date of creation, etc.) and a back-link to the index page.
new.html.erb
and edit.html.erb
- Should include a form to create or update a task.
Notice that creating (as well as updating) a task is a 2-requests process:
- A first GET request is here to display the HTML form
- A second POST or PATCH request enables to actually create or update the task using the parameters submitted in the form.
An action is not necessarily associated with a view. For instance, the create/update/destroy actions are not associated with any views. They are just here to perform operations on the DB and then redirect_to
another URL.
When you are done with the exercise, which means you have a functional todo-app, refactor your code:
- Use a partial to factor the new and edit HTML forms.
- Use the
form_for
helper to build you new/edit form. - Refactor your routes with the
resources
routing method.