質問編集履歴
3
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -124,12 +124,12 @@
|
|
124
124
|
= render partial: 'footer.html.haml'
|
125
125
|
```
|
126
126
|
**book.rb**
|
127
|
+
```
|
127
|
-
|
128
|
+
class Book < ApplicationRecord
|
128
129
|
validates :name, presence: true
|
129
130
|
validates :price, presence: true
|
130
131
|
validates :image, presence: true
|
131
132
|
validates :author, presence: true
|
132
|
-
validates :category_id, presence: true
|
133
133
|
|
134
134
|
belongs_to :user
|
135
135
|
has_many :categories
|
@@ -139,6 +139,8 @@
|
|
139
139
|
**category.rb**
|
140
140
|
```
|
141
141
|
class Category < ApplicationRecord
|
142
|
+
validates :name, presence: true
|
143
|
+
|
142
144
|
has_many :books
|
143
145
|
has_many :books, through: :books_categories
|
144
146
|
end
|
@@ -146,6 +148,10 @@
|
|
146
148
|
**book_category.rb**
|
147
149
|
```
|
148
150
|
class BookCategory < ApplicationRecord
|
151
|
+
validates :book_id, presence: true
|
152
|
+
validates :category_id, presence: true
|
153
|
+
|
154
|
+
|
149
155
|
belongs_to :book
|
150
156
|
belongs_to :category
|
151
157
|
end
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -123,6 +123,51 @@
|
|
123
123
|
%footer
|
124
124
|
= render partial: 'footer.html.haml'
|
125
125
|
```
|
126
|
+
**book.rb**
|
127
|
+
```class Book < ApplicationRecord
|
128
|
+
validates :name, presence: true
|
129
|
+
validates :price, presence: true
|
130
|
+
validates :image, presence: true
|
131
|
+
validates :author, presence: true
|
132
|
+
validates :category_id, presence: true
|
133
|
+
|
134
|
+
belongs_to :user
|
135
|
+
has_many :categories
|
136
|
+
has_many :categories, through: :books_categories
|
137
|
+
end
|
138
|
+
```
|
139
|
+
**category.rb**
|
140
|
+
```
|
141
|
+
class Category < ApplicationRecord
|
142
|
+
has_many :books
|
143
|
+
has_many :books, through: :books_categories
|
144
|
+
end
|
145
|
+
```
|
146
|
+
**book_category.rb**
|
147
|
+
```
|
148
|
+
class BookCategory < ApplicationRecord
|
149
|
+
belongs_to :book
|
150
|
+
belongs_to :category
|
151
|
+
end
|
152
|
+
```
|
153
|
+
**user.rb**
|
154
|
+
```
|
155
|
+
class User < ApplicationRecord
|
156
|
+
# Include default devise modules. Others available are:
|
157
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
158
|
+
devise :database_authenticatable, :registerable,
|
159
|
+
:recoverable, :rememberable, :validatable
|
160
|
+
end
|
161
|
+
|
162
|
+
```
|
163
|
+
**application_record.rb**
|
164
|
+
```
|
165
|
+
class ApplicationRecord < ActiveRecord::Base
|
166
|
+
self.abstract_class = true
|
167
|
+
end
|
168
|
+
|
169
|
+
```
|
170
|
+
|
126
171
|
#自分で調べたことや試したこと
|
127
172
|
・ 下記category_idの記述の仕方が違うと判断し、検索して出てきた記述を書いてみたが全て違った
|
128
173
|
> params.require(:book).permit(:name, :price, :author, :image, :user_id, { category_id: [] })
|
1
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -24,21 +24,71 @@
|
|
24
24
|
#該当のソースコード
|
25
25
|
**books_controller.rb**
|
26
26
|
```
|
27
|
+
class BooksController < ApplicationController
|
28
|
+
before_action :move_to_index, except: [:index, :show]
|
29
|
+
before_action :set_book, only: [:edit, :show]
|
30
|
+
|
31
|
+
def index
|
32
|
+
@books = Book.all.order(created_at: :DESC)
|
33
|
+
end
|
34
|
+
|
35
|
+
def new
|
36
|
+
@book = Book.new
|
37
|
+
end
|
38
|
+
|
27
39
|
def create
|
40
|
+
ActiveRecord::Base.transaction do
|
28
|
-
|
41
|
+
book = Book.create!(book_params)
|
42
|
+
BookCategory.create!(book: book, category_id: params[:category])
|
43
|
+
end
|
44
|
+
redirect_to root_path
|
45
|
+
rescue ActiveRecord::RecordInvalid => e
|
46
|
+
render :new
|
47
|
+
end
|
48
|
+
|
49
|
+
def destroy
|
50
|
+
book = Book.find(params[:id])
|
51
|
+
book.destroy
|
29
|
-
if
|
52
|
+
if !book.save
|
30
53
|
redirect_to root_path
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def show
|
58
|
+
end
|
59
|
+
|
60
|
+
def edit
|
61
|
+
end
|
62
|
+
|
63
|
+
def update
|
64
|
+
book = Book.find(params[:id])
|
65
|
+
book.update(book_params)
|
66
|
+
if book.save
|
67
|
+
redirect_to book_path(book.id)
|
31
68
|
else
|
32
|
-
render :
|
69
|
+
render :index
|
33
70
|
end
|
34
71
|
end
|
35
72
|
|
73
|
+
|
36
74
|
private
|
37
75
|
|
38
76
|
def book_params
|
77
|
+
params
|
39
|
-
|
78
|
+
.require(:book).permit(:name, :price, :author, :image)
|
79
|
+
.merge(user_id: current_user.id)
|
40
80
|
end
|
41
81
|
|
82
|
+
def set_book
|
83
|
+
@book = Book.find(params[:id])
|
84
|
+
end
|
85
|
+
|
86
|
+
def move_to_index
|
87
|
+
unless user_signed_in?
|
88
|
+
redirect_to action: :index
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
42
92
|
```
|
43
93
|
**new.html.haml**
|
44
94
|
投稿する画面のviewです。
|