Skip to content

4.2 Projects Controller

Nicolas Erlichman edited this page Aug 18, 2022 · 5 revisions

We have our Project model but we are missing a controller to perform actions. We will create a controller with a few endpoints to handle the create, read, update and delete operations.

We will run the following command:

foo@bar:~$ rails generate controller Api::Project
      create  app/controllers/api/project_controller.rb
      invoke  test_unit
      create    test/controllers/api/project_controller_test.rb

We go now to the file app/controllers/api/project_controller.rb.

Replace the parent class with ApiController:

class Api::ProjectController < ApiController

Create

We will create an endpoint to allow users to create a project for their company.

Let's add this block to the controller

def create
  project = Project.create!(project_params.merge(company: current_user.company))
  render json: project
end

private

def project_params
  params.require(:project).permit(:name, :description, :start_date)
end

Index

We will create an endpoint to allow users to get all of their company projects information.

Let's add this block to the controller:

def index
  render json: current_user.company.projects
end

Read

We will create an endpoint to allow users to get the information of a specific project.

Let's add this block to the controller:

def show
  render json: current_user.company.projects.find(project_id_param)
end

private

def project_id_param
  params[:id]
end

Update

We will create an endpoint to allow users to update one of their company projects information.

Let's add this block to the controller:

def update
  project = current_user.company.projects.find(project_id_param)
  project.update!(project_params)
  render json: project
end

Destroy

We will create an endpoint to allow users to destroy a project.

Let's add this block to the controller:

def destroy
  current_user.company.projects.find(project_id_param).destroy!
  head :ok
end

We have everything ready to be attached to API endpoints.

Let's go to the file config/routes.rb now and add this block as follows:

Rails.application.routes.draw do
  ...
  namespace :api, defaults: { format: :json } do
    resource :company, only: %i[show update create destroy]
    resources :projects, only: %i[index show update create destroy]
  end
  ...
end

If we run the command rails routes we should get the routes we just added:

foo@bar:~$ rails routes
api_projects GET    /api/projects(.:format)                   api/projects#index {:format=>:json}
             POST   /api/projects(.:format)                   api/projects#create {:format=>:json}
api_project  GET    /api/projects/:id(.:format)               api/projects#show {:format=>:json}
             PATCH  /api/projects/:id(.:format)               api/projects#update {:format=>:json}
             PUT    /api/projects/:id(.:format)               api/projects#update {:format=>:json}
             DELETE /api/projects/:id(.:format)               api/projects#destroy {:format=>:json}

Clone this wiki locally