質問編集履歴

3

修正

2020/09/05 11:48

投稿

ntk__7__ksn
ntk__7__ksn

スコア14

test CHANGED
File without changes
test CHANGED
@@ -250,7 +250,9 @@
250
250
 
251
251
  **book.rb**
252
252
 
253
+ ```
254
+
253
- ```class Book < ApplicationRecord
255
+ class Book < ApplicationRecord
254
256
 
255
257
  validates :name, presence: true
256
258
 
@@ -260,39 +262,49 @@
260
262
 
261
263
  validates :author, presence: true
262
264
 
265
+
266
+
267
+ belongs_to :user
268
+
269
+ has_many :categories
270
+
271
+ has_many :categories, through: :books_categories
272
+
273
+ end
274
+
275
+ ```
276
+
277
+ **category.rb**
278
+
279
+ ```
280
+
281
+ class Category < ApplicationRecord
282
+
283
+ validates :name, presence: true
284
+
285
+
286
+
287
+ has_many :books
288
+
289
+ has_many :books, through: :books_categories
290
+
291
+ end
292
+
293
+ ```
294
+
295
+ **book_category.rb**
296
+
297
+ ```
298
+
299
+ class BookCategory < ApplicationRecord
300
+
301
+ validates :book_id, presence: true
302
+
263
303
  validates :category_id, presence: true
264
304
 
265
305
 
266
306
 
267
- belongs_to :user
307
+
268
-
269
- has_many :categories
270
-
271
- has_many :categories, through: :books_categories
272
-
273
- end
274
-
275
- ```
276
-
277
- **category.rb**
278
-
279
- ```
280
-
281
- class Category < ApplicationRecord
282
-
283
- has_many :books
284
-
285
- has_many :books, through: :books_categories
286
-
287
- end
288
-
289
- ```
290
-
291
- **book_category.rb**
292
-
293
- ```
294
-
295
- class BookCategory < ApplicationRecord
296
308
 
297
309
  belongs_to :book
298
310
 

2

修正

2020/09/05 11:48

投稿

ntk__7__ksn
ntk__7__ksn

スコア14

test CHANGED
File without changes
test CHANGED
@@ -248,6 +248,96 @@
248
248
 
249
249
  ```
250
250
 
251
+ **book.rb**
252
+
253
+ ```class Book < ApplicationRecord
254
+
255
+ validates :name, presence: true
256
+
257
+ validates :price, presence: true
258
+
259
+ validates :image, presence: true
260
+
261
+ validates :author, presence: true
262
+
263
+ validates :category_id, presence: true
264
+
265
+
266
+
267
+ belongs_to :user
268
+
269
+ has_many :categories
270
+
271
+ has_many :categories, through: :books_categories
272
+
273
+ end
274
+
275
+ ```
276
+
277
+ **category.rb**
278
+
279
+ ```
280
+
281
+ class Category < ApplicationRecord
282
+
283
+ has_many :books
284
+
285
+ has_many :books, through: :books_categories
286
+
287
+ end
288
+
289
+ ```
290
+
291
+ **book_category.rb**
292
+
293
+ ```
294
+
295
+ class BookCategory < ApplicationRecord
296
+
297
+ belongs_to :book
298
+
299
+ belongs_to :category
300
+
301
+ end
302
+
303
+ ```
304
+
305
+ **user.rb**
306
+
307
+ ```
308
+
309
+ class User < ApplicationRecord
310
+
311
+ # Include default devise modules. Others available are:
312
+
313
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
314
+
315
+ devise :database_authenticatable, :registerable,
316
+
317
+ :recoverable, :rememberable, :validatable
318
+
319
+ end
320
+
321
+
322
+
323
+ ```
324
+
325
+ **application_record.rb**
326
+
327
+ ```
328
+
329
+ class ApplicationRecord < ActiveRecord::Base
330
+
331
+ self.abstract_class = true
332
+
333
+ end
334
+
335
+
336
+
337
+ ```
338
+
339
+
340
+
251
341
  #自分で調べたことや試したこと
252
342
 
253
343
  ・ 下記category_idの記述の仕方が違うと判断し、検索して出てきた記述を書いてみたが全て違った

1

修正

2020/09/05 11:04

投稿

ntk__7__ksn
ntk__7__ksn

スコア14

test CHANGED
File without changes
test CHANGED
@@ -50,21 +50,97 @@
50
50
 
51
51
  ```
52
52
 
53
+ class BooksController < ApplicationController
54
+
55
+ before_action :move_to_index, except: [:index, :show]
56
+
57
+ before_action :set_book, only: [:edit, :show]
58
+
59
+
60
+
61
+ def index
62
+
63
+ @books = Book.all.order(created_at: :DESC)
64
+
65
+ end
66
+
67
+
68
+
69
+ def new
70
+
71
+ @book = Book.new
72
+
73
+ end
74
+
75
+
76
+
53
77
  def create
54
78
 
79
+ ActiveRecord::Base.transaction do
80
+
55
- Book.create(book_params)
81
+ book = Book.create!(book_params)
82
+
56
-
83
+ BookCategory.create!(book: book, category_id: params[:category])
84
+
85
+ end
86
+
87
+ redirect_to root_path
88
+
89
+ rescue ActiveRecord::RecordInvalid => e
90
+
91
+ render :new
92
+
93
+ end
94
+
95
+
96
+
97
+ def destroy
98
+
99
+ book = Book.find(params[:id])
100
+
101
+ book.destroy
102
+
57
- if Book.create
103
+ if !book.save
58
104
 
59
105
  redirect_to root_path
60
106
 
107
+ end
108
+
109
+ end
110
+
111
+
112
+
113
+ def show
114
+
115
+ end
116
+
117
+
118
+
119
+ def edit
120
+
121
+ end
122
+
123
+
124
+
125
+ def update
126
+
127
+ book = Book.find(params[:id])
128
+
129
+ book.update(book_params)
130
+
131
+ if book.save
132
+
133
+ redirect_to book_path(book.id)
134
+
61
135
  else
62
136
 
63
- render :new
137
+ render :index
64
-
138
+
65
- end
139
+ end
66
-
140
+
67
- end
141
+ end
142
+
143
+
68
144
 
69
145
 
70
146
 
@@ -74,11 +150,35 @@
74
150
 
75
151
  def book_params
76
152
 
153
+ params
154
+
77
- params.require(:book).permit(:name, :price, :author, :image, :user_id, { category_id: [] })
155
+ .require(:book).permit(:name, :price, :author, :image)
156
+
78
-
157
+ .merge(user_id: current_user.id)
158
+
79
- end
159
+ end
160
+
161
+
162
+
80
-
163
+ def set_book
164
+
81
-
165
+ @book = Book.find(params[:id])
166
+
167
+ end
168
+
169
+
170
+
171
+ def move_to_index
172
+
173
+ unless user_signed_in?
174
+
175
+ redirect_to action: :index
176
+
177
+ end
178
+
179
+ end
180
+
181
+ end
82
182
 
83
183
  ```
84
184