・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction ・ Introduction of Bootstrap3 ・ Implementation of posting function
** ① Create a category model table **
Terminal
$ rails g model Category name:string
Terminal
$ rails db:migrate
schema.rb
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
** ② Add a column to the books table **
Terminal
$ rails g migration AddCategoryIdToBooks category_id:integer
Terminal
$ rails db:migrate
schema.rb
create_table "books", force: :cascade do |t|
t.integer "user_id"
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
** ③ Edit each model **
book.rb
#Postscript
has_many :categories, dependent: :destroy
dependent: :destroy
➡︎ When the corresponding category is deleted, the linked book is also deleted.
category.rb
#Postscript
belongs_to :book
** ① Create and edit categories_controller.rb
**
Create index and edit views in advance.
Terminal
$ rails g controller categories index edit
categories_controller.rb
class CategoriesController < ApplicationController
before_action :set_category, only: [:edit, :update, :destroy]
def index
@category = Category.new
@categories = Category.all
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to categories_path
else
@categories = Category.all
render 'index'
end
end
def edit
end
def update
if @category.update(category_params)
redirect_to categories_path
else
render 'edit'
end
end
def destroy
@category.destroy
redirect_to categories_path
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name)
end
end
** ② Add category_id
to the strong parameter of books_controller.rb
**
books_controller.rb
def book_params
params.require(:book).permit(:title, :body, :category_id)
end
routes.rb
#Postscript
resources :categories, except: [:new, :show]
except: [:new, :show]
➡︎ Add actions other than new and show to the routing.
slim:categories/index.html.slim
.row
.col-xs-3
= form_with model: @category, local: true do |f|
= f.label :name, 'Category name'
br
= f.text_field :name, class:'form-control'
br
= f.submit 'add to', class: 'btn btn-primary btn-block'
.col-xs-9
table.table
thead
tr
th
|Category name
th
th
tbody
- @categories.each do |category|
tr
td
= category.name
td
= link_to 'Edit', edit_category_path(category), class: 'btn-sm btn-primary'
td
= link_to 'Delete', category_path(category), method: :delete, data: { confirm: '本当にDeleteしますか?' }, class: 'btn-sm btn-danger'
slim:categories/edit.html.slim
.row
.col-xs-3
.col-xs-6
= form_with model: @category, local: true do |f|
= f.label :name, 'Category name'
br
= f.text_field :name, class:'form-control'
br
= f.submit 'Save', class: 'btn btn-primary btn-block'
.col-xs-3
slim:books/index.html.slim
.row
.col-xs-3
= form_with model: @book, local: true do |f|
= f.label :title, 'title'
br
= f.text_field :title, class:'form-control'
br
= f.label :body, 'Text'
br
= f.text_area :body, class:'form-control'
br
/Postscript
= f.label :category_id, 'Category'
br
= f.collection_select :category_id, Category.all, :id, :name, { prompt: 'Please select' }, class: 'form-control'
br
= f.submit 'Post', class: 'btn btn-primary btn-block'
.col-xs-9
table.table
thead
tr
th
|Contributor
th
|title
th
|Text
/Postscript
th
|Category
th
tbody
- @books.each do |book|
tr
td
= link_to book.user do
= book.user.name
td
= link_to book.title, book_path(book)
td
= book.body
/Postscript
td
= book.category.name
td
-if book.user == current_user
= link_to 'Delete', book, method: :delete, data: { confirm: '本当にDeleteしてもよろしいですか?' }, class: 'btn-sm btn-danger'
= f.collection_select :category_id, Category.all, :id, :name
➡︎ The name
of all categories is displayed in the pull-down menu, and the value is set to ʻid`.
Recommended Posts