質問編集履歴

4

books_controller.rbのcreateアクションを変更しました。

2019/11/21 13:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -90,7 +90,7 @@
90
90
 
91
91
  <tr>
92
92
 
93
- <th>プロフィール</th>
93
+ <th></th>
94
94
 
95
95
  <th>Title</th>
96
96
 
@@ -108,7 +108,7 @@
108
108
 
109
109
  <tr>
110
110
 
111
- <td>プロフィール</td>
111
+ <td>名前、写真</td>
112
112
 
113
113
  <td><%= book.title %></td>
114
114
 
@@ -186,6 +186,8 @@
186
186
 
187
187
  @users = User.all
188
188
 
189
+ @book = Book.new
190
+
189
191
  end
190
192
 
191
193
 
@@ -210,6 +212,18 @@
210
212
 
211
213
 
212
214
 
215
+ def update
216
+
217
+ @user = User.find(params[:id])
218
+
219
+ user.update(user_params)
220
+
221
+ redirect_to user_path(@user)
222
+
223
+ end
224
+
225
+
226
+
213
227
  private
214
228
 
215
229
 
@@ -232,11 +246,7 @@
232
246
 
233
247
  class BooksController < ApplicationController
234
248
 
235
-
236
-
237
- def new
249
+ before_action :authenticate_user!, only: [:show]
238
-
239
- end
240
250
 
241
251
 
242
252
 
@@ -244,6 +254,8 @@
244
254
 
245
255
  book = Book.new(book_params)
246
256
 
257
+ book.user = current_user
258
+
247
259
  book.save
248
260
 
249
261
  redirect_to '/books/:id'
@@ -254,6 +266,8 @@
254
266
 
255
267
  def index
256
268
 
269
+
270
+
257
271
  end
258
272
 
259
273
 
@@ -274,6 +288,18 @@
274
288
 
275
289
 
276
290
 
291
+ def update
292
+
293
+ @book = Book.find(params[:id])
294
+
295
+ book.update(book_params)
296
+
297
+ redirect_to user_path(@user)
298
+
299
+ end
300
+
301
+
302
+
277
303
  private
278
304
 
279
305
 

3

モデルを追加しました

2019/11/21 13:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -289,3 +289,45 @@
289
289
  end
290
290
 
291
291
  ```
292
+
293
+
294
+
295
+ user.rb
296
+
297
+ ```
298
+
299
+ class User < ApplicationRecord
300
+
301
+ # Include default devise modules. Others available are:
302
+
303
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
304
+
305
+ devise :database_authenticatable, :registerable,
306
+
307
+ :recoverable, :rememberable, :trackable, :validatable
308
+
309
+
310
+
311
+ has_many :books, dependent: :destroy
312
+
313
+
314
+
315
+ attachment :profile_image
316
+
317
+ end
318
+
319
+ ```
320
+
321
+
322
+
323
+ book.rb
324
+
325
+ ```
326
+
327
+ class Book < ApplicationRecord
328
+
329
+ belongs_to :user
330
+
331
+ end
332
+
333
+ ```

2

books_controller.rbを追加しました。

2019/11/18 14:34

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -223,3 +223,69 @@
223
223
  end
224
224
 
225
225
  ```
226
+
227
+
228
+
229
+ books_controller.rb
230
+
231
+ ```
232
+
233
+ class BooksController < ApplicationController
234
+
235
+
236
+
237
+ def new
238
+
239
+ end
240
+
241
+
242
+
243
+ def create
244
+
245
+ book = Book.new(book_params)
246
+
247
+ book.save
248
+
249
+ redirect_to '/books/:id'
250
+
251
+ end
252
+
253
+
254
+
255
+ def index
256
+
257
+ end
258
+
259
+
260
+
261
+ def show
262
+
263
+ @book = Book.find(params[:id])
264
+
265
+ end
266
+
267
+
268
+
269
+ def edit
270
+
271
+ @book = Book.find(params[:id])
272
+
273
+ end
274
+
275
+
276
+
277
+ private
278
+
279
+
280
+
281
+ def book_params
282
+
283
+ params.require(:book).permit(:title, :body)
284
+
285
+ end
286
+
287
+
288
+
289
+ end
290
+
291
+ ```

1

文章を編集しました。

2019/11/18 11:21

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -174,65 +174,51 @@
174
174
 
175
175
 
176
176
 
177
- books_controller.rb
177
+ users_controller.rb
178
-
178
+
179
- ```
179
+ ```
180
-
180
+
181
- class BooksController < ApplicationController
181
+ class UsersController < ApplicationController
182
+
183
+ def index
184
+
185
+ @user = current_user
186
+
187
+ @users = User.all
188
+
189
+ end
182
190
 
183
191
 
184
192
 
185
- def new
193
+ def show
194
+
195
+ @user = User.find(params[:id])
196
+
197
+ @book = Book.new
198
+
199
+ @books = @user.books
200
+
201
+ end
202
+
203
+
204
+
205
+ def edit
206
+
207
+ @user = User.find(params[:id])
186
208
 
187
209
  end
188
210
 
189
211
 
190
212
 
191
- def create
192
-
193
- book = Book.new(book_params)
194
-
195
- book.save
196
-
197
- redirect_to '/books/:id'
198
-
199
- end
200
-
201
-
202
-
203
- def index
204
-
205
- end
206
-
207
-
208
-
209
- def show
210
-
211
- @book = Book.find(params[:id])
212
-
213
- end
214
-
215
-
216
-
217
- def edit
218
-
219
- @book = Book.find(params[:id])
220
-
221
- end
222
-
223
-
224
-
225
213
  private
226
214
 
227
-
228
-
215
+
216
+
229
- def book_params
217
+ def user_params
230
-
218
+
231
- params.require(:book).permit(:title, :body)
219
+ params.require(:user).permit(:name, :introduction, :profile_image_id)
232
-
220
+
233
- end
221
+ end
234
-
235
-
236
222
 
237
223
  end
238
224