Building a Project Management App with Ruby on Rails 7 and ChatGPT Integration - Part 2: Controllers, Views, and OpenAI API Integration

Coding Posted on Jun 15, 2023
Welcome back to my blog series on building a project management app with Ruby on Rails 7 and ChatGPT integration! In this post, we'll create controllers and views for our app, integrate with the OpenAI API, and implement project briefing and project plan creation functionalities. If you haven't yet, start with Part 1 where we begin by setting up the project and OpenAI API.

Step 1: Creating Controllers for Project Briefings and Project Plans


Generate controllers for ProjectBriefings and ProjectPlans:

rails generate controller ProjectBriefings
rails generate controller ProjectPlans

Step 2: Setting up Routes


Update your config/routes.rb file with the following:

Rails.application.routes.draw do
  devise_for :users

  resources :project_briefings
  resources :project_plans

  root to: 'project_briefings#index'
end

Step 3: Creating Views for Project Briefings and Project Plans


Create views for listing, creating, and editing project briefings and project plans. You can use Rails' scaffold generator as a reference to create views with basic CRUD functionality:

rails generate scaffold_controller ProjectBriefing --skip
rails generate scaffold_controller ProjectPlan --skip

Copy the generated views from app/views/project_briefings and app/views/project_plans into their respective folders.

Step 4: Integrating OpenAI API


Create a new service called OpenaiService:

mkdir app/services
touch app/services/openai_service.rb

Add the following code to app/services/openai_service.rb:

class OpenaiService
  def self.create_document(prompt)
    Openai::Completion.create(
      engine: "text-davinci-002",
      prompt: prompt,
      max_tokens: 500,
      n: 1,
      stop: nil,
      temperature: 0.7
    )
  end
end

Step 5: Implementing Project Briefing and Project Plan Creation


Update the ProjectBriefingsController and ProjectPlansController to integrate with the OpenAI API when creating a new project briefing or project plan:

class ProjectBriefingsController < ApplicationController
  # ...

  def create
    @project_briefing = current_user.project_briefings.build(project_briefing_params)

    prompt = "Create a project briefing for a software project with the following title and description: #{project_briefing_params[:title]} - #{project_briefing_params[:description]}"
    response = OpenaiService.create_document(prompt)
    @project_briefing.description = response.choices.first.text

    if @project_briefing.save
      redirect_to @project_briefing, notice: 'Project briefing was successfully created.'
    else
      render :new
    end
  end

  # ...
end

class ProjectPlansController < ApplicationController
  # ...

  def create
    @project_plan = current_user.project_plans.build(project_plan_params)

    prompt = "Create a project plan for a software project with the following title and description: #{project_plan_params[:title]} - #{project_plan_params[:description]}"
    response = OpenaiService.create_document(prompt)
    @project_plan.description = response.choices.first.text

    if @project_plan.save
      redirect_to @project_plan, notice: 'Project plan was successfully created.'
    else
      render :new
    end
  end

  # ...
end

That wraps up Part 2 of our blog series! In this post, we created controllers and views for project briefings and project plans using Simple Form for our forms. We integrated with the OpenAI API to draft comprehensive documents and implemented project briefing and project plan creation functionalities.

In the next post, we'll add user authentication to protect our project briefings and project plans, improve the user interface using Bootstrap, and deploy our application to a production environment. Don't miss the exciting conclusion of our series, and thank you for joining us on this journey!

Leave a comment:

Comments (0)