[Ruby] Task management application creation procedure

4 minute read

1. Application preparation

1-1. Think about the contents of the application to be created

Function Function content
List display List all tasks
Detailed display Display the detailed contents of one task
New registration Register new task in db
Edit Edit registered tasks and update db
Delete Delete registered task from db

1-2. Decide the application name and create a template

rails new application name [optional]

# Example)

rails new taskmanage -d postgresql

1-3. Create db

Go to the directory where you created the application and create db

rails db:create

1-4. Try to start the server

Hit the following command, “Http://localhost:3000” To see if the default rails page is displayed

rails s

2. Create a model

Model components

  • Ruby class corresponding to the model Camel case

  • Database table corresponding to the model A model with multiple class names, a snake case

2-1. Design model attributes

Attribute | Attribute name | Data type | Whether to allow null | Default value | |:——–|:——-|:———–|:————–|: ———–:| | Name | name | string | Do not allow | None | | Detailed description | description | text | Allow | None |

2-2. Create a model template

rails g model [model name] [attribute name: data type attribute name: data type ...] [option]

# Example)

rails g model Task name:string description:text

2-3. Migrate and add table to db

Check the created migration file and execute rails db:migrate

3. Create controller and view

rails g controller controller name [action name action name...] [option]

# Example)

rails g controller tasks index show new edit
Rails.application.routes.draw do
 root to:'tasks#index'
 resources :tasks
end

3. Implementation of new registration function

= link_to'New registration', new_task_path

3-2. Implement action for new registration screen

tasks_controller.rb


def new
 @task = Task.new
end

3-3. Implement new registration page view

slim:new.html.slim


= form_with model: @task, local: true do |f|
 .form-group
  = f.label :name #label = Focus on the input field by displaying the name of the input field and clicking the label part
  = f.text_field :name
 .form-group
  = f.label :description
  = f.text_area :description
 = f.submit nil

3-4. Implementation of registration action

create action = “Save the data sent from the registration form in db and transition to the list screen”

tasks_controller.rb


def create
 @task= Task.new(task_params)
 task.save
 redirect_to tasks_url, notice: "The task "#{task.name}" has been registered. "
end
private
 def task_params #Strong parameters = role to check whether the information sent from the form as request parameters is in the form of {task: {...}} as expected, and extract only the necessary information
  params.require(:task).permit(:name, :description)
 end

Difference between render and Redirect_to

| render | Redirect_to | |:—————–|:——————| | Display the view following the action | Direct the URL to another URL without displaying the view immediately after processing the action |

flash message

When redirecting, convey a bit of data for the next request

 redirect_to tasks_url, notice: "The task "#{task.name}" has been registered. "
# Meaning to redirect after task registration completion message

flash.now[:alert] = "Immediate alert"
# If in the same request, flash.now

4. Implementation of list display function

4-1. Implement action for new registration screen

tasks_controller.rb


def index
 @tasks = Task.all
end

4-2. Show all tasks on the list page

@tasks.each do |task|
 task.name
 task.created_at

5. Implementation of detailed display function

ruby:index.html.slim


link_to task.name, task_path(task)

5-2. Get the selected task by the detailed display action

tasks_controller.rb


def show
 @task = Task.find(params[:id])
end

5-3. Get the selected task by the detailed display action

ruby:show.html.slim


@task.id
@task.name
@task.created_at
@task.updated_at

6. Editing function implementation

ruby:index.html.slim


link_to'edit', edit_task_path(task)

ruby:show.html.slim


link_to'edit', edit_task_path

6-2. Defining edit and update actions

tasks_controller.rb


def edit
 @task = Task.find(params[:id])
end

def update
 task = Task.find(params[:id])
 task.update!(task_params)
 redirect_to tasks_url, notice: "The task "#{task.name}" has been updated. "
end

6-3. Implemented edit page view

slim:new.html.slim


= form_with model: @task, local: true do |f|
 .form-group
  = f.label :name #label = Focus on the input field by displaying the name of the input field and clicking the label part
  = f.text_field :name
 .form-group
  = f.label :description
  = f.text_area :description
 = f.submit nil

Standardization using partial templates

Use a partial template to put together the common parts of the new registration page and edit page

ruby:_form.html.slim


= form_with model: task, local: true do |f|
 .form-group
  = f.label :name
  = f.text_field :name
 .form-group
  = f.label :description
  = f.text_area :description
 = f.submit nil

Invoking a partial template

ruby:new.html.slim


= render partial:'form'. locals: {task: @task} # "Pass instance variable @task as local variable task in partial"

ruby:edit.html.slim


= render partial:'form'. locals: {task: @task}

7. Implementation of delete function

7-1. Add delete button to list page and detail page

ruby:index.html.slim


= link_to'Delete', task, method: :delete, date: {confirm: "Delete the task "#{task.name}". Is it OK? "}

ruby:show.html.slim


= link_to'Delete', @task, method: :delete, date: {confirm: "Delete the task "#{task.name}". Is it OK? "}

7-2. Define destroy action

tasks_controller.rb

```ruby

def destroy task = Task.find(params[:id]) task.destroy redirect_to tasks_url, notice: “Task “#{task.name}” has been deleted. “ end ```[Reference | Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E4%BD%BF%E3%81%88%E3%82%8B-Ruby-Rails-5%E9%80%9F%E7%BF%92%E5%AE%9F%E8%B7%B5%E3%82%AC(%E3%82%A4%E3%83%89-%E5%A4%A7%E5%A0%B4%E5%AF%A7%E5%AD%90/dp/4839962227)