質問編集履歴
4
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -468,6 +468,60 @@
|
|
468
468
|
|
469
469
|
|
470
470
|
|
471
|
+
### 該当のソースコード
|
472
|
+
|
473
|
+
[posts/new.html.erb]
|
474
|
+
|
475
|
+
<div class="main-posts-new">
|
476
|
+
|
477
|
+
<div class="container">
|
478
|
+
|
479
|
+
<h1 class="form-heading">本を要約してメモする</h1><br>
|
480
|
+
|
481
|
+
<h4>本のタイトル・作者</h4>
|
482
|
+
|
483
|
+
<%= form_tag("/posts/create", {multipart: true}) do %>
|
484
|
+
|
485
|
+
<div class="form">
|
486
|
+
|
487
|
+
<div class="form-body">
|
488
|
+
|
489
|
+
<% @post.errors.full_messages.each do |message| %>
|
490
|
+
|
491
|
+
<div class="form-error">
|
492
|
+
|
493
|
+
<%= message %>
|
494
|
+
|
495
|
+
</div>
|
496
|
+
|
497
|
+
<% end %>
|
498
|
+
|
499
|
+
<textarea name="content"><%= @post.content %></textarea>
|
500
|
+
|
501
|
+
<h4>画像</h4>
|
502
|
+
|
503
|
+
<input name="image" type="file">
|
504
|
+
|
505
|
+
<h4>要約スペース</h4>
|
506
|
+
|
507
|
+
<textarea name="content"><%= @post.content %></textarea><br>
|
508
|
+
|
509
|
+
<input type="submit" value="投稿">
|
510
|
+
|
511
|
+
</div>
|
512
|
+
|
513
|
+
</div>
|
514
|
+
|
515
|
+
<% end %>
|
516
|
+
|
517
|
+
</div>
|
518
|
+
|
519
|
+
</div>
|
520
|
+
|
521
|
+
|
522
|
+
|
523
|
+
|
524
|
+
|
471
525
|
|
472
526
|
|
473
527
|
```ここに言語名を入力
|
3
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -350,51 +350,49 @@
|
|
350
350
|
|
351
351
|
|
352
352
|
|
353
|
-
|
353
|
+
Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
|
354
354
|
|
355
355
|
gem 'rails', '~> 6.0.1'
|
356
356
|
|
357
|
-
|
357
|
+
Use sqlite3 as the database for Active Record
|
358
358
|
|
359
359
|
gem 'sqlite3', '~> 1.4'
|
360
360
|
|
361
|
-
|
361
|
+
Use Puma as the app server
|
362
362
|
|
363
363
|
gem 'puma', '~> 4.1'
|
364
364
|
|
365
|
-
|
365
|
+
Use SCSS for stylesheets
|
366
366
|
|
367
367
|
gem 'sass-rails', '>= 6'
|
368
368
|
|
369
|
-
|
369
|
+
Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
|
370
370
|
|
371
371
|
gem 'webpacker', '~> 4.0'
|
372
372
|
|
373
|
-
|
373
|
+
Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
|
374
374
|
|
375
375
|
gem 'turbolinks', '~> 5'
|
376
376
|
|
377
|
-
|
377
|
+
Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
|
378
378
|
|
379
379
|
gem 'jbuilder', '~> 2.7'
|
380
380
|
|
381
|
-
|
381
|
+
Use Redis adapter to run Action Cable in production
|
382
|
-
|
382
|
+
|
383
|
-
|
383
|
+
gem 'redis', '~> 4.0'
|
384
|
-
|
384
|
+
|
385
|
-
|
385
|
+
Use Active Model has_secure_password
|
386
|
-
|
386
|
+
|
387
|
-
|
387
|
+
gem 'bcrypt', '~> 3.1.7'
|
388
|
-
|
389
|
-
|
390
|
-
|
388
|
+
|
391
|
-
|
389
|
+
Use Active Storage variant
|
392
|
-
|
390
|
+
|
393
|
-
|
391
|
+
gem 'image_processing', '~> 1.2'
|
394
|
-
|
395
|
-
|
396
|
-
|
392
|
+
|
393
|
+
|
394
|
+
|
397
|
-
|
395
|
+
Reduces boot times through caching; required in config/boot.rb
|
398
396
|
|
399
397
|
gem 'bootsnap', '>= 1.4.2', require: false
|
400
398
|
|
@@ -402,7 +400,7 @@
|
|
402
400
|
|
403
401
|
group :development, :test do
|
404
402
|
|
405
|
-
|
403
|
+
Call 'byebug' anywhere in the code to stop execution and get a debugger console
|
406
404
|
|
407
405
|
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
|
408
406
|
|
@@ -412,13 +410,13 @@
|
|
412
410
|
|
413
411
|
group :development do
|
414
412
|
|
415
|
-
|
413
|
+
Access an interactive console on exception pages or by calling 'console' anywhere in the code.
|
416
414
|
|
417
415
|
gem 'web-console', '>= 3.3.0'
|
418
416
|
|
419
417
|
gem 'listen', '>= 3.0.5', '< 3.2'
|
420
418
|
|
421
|
-
|
419
|
+
Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
|
422
420
|
|
423
421
|
gem 'spring'
|
424
422
|
|
@@ -430,13 +428,13 @@
|
|
430
428
|
|
431
429
|
group :test do
|
432
430
|
|
433
|
-
|
431
|
+
Adds support for Capybara system testing and selenium driver
|
434
432
|
|
435
433
|
gem 'capybara', '>= 2.15'
|
436
434
|
|
437
435
|
gem 'selenium-webdriver'
|
438
436
|
|
439
|
-
|
437
|
+
Easy installation and use of web drivers to run system tests with browsers
|
440
438
|
|
441
439
|
gem 'webdrivers'
|
442
440
|
|
@@ -444,7 +442,7 @@
|
|
444
442
|
|
445
443
|
|
446
444
|
|
447
|
-
|
445
|
+
Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
448
446
|
|
449
447
|
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
|
450
448
|
|
2
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,6 +62,416 @@
|
|
62
62
|
|
63
63
|
|
64
64
|
|
65
|
+
### 該当のソースコード
|
66
|
+
|
67
|
+
**[posts_controller.rb]**
|
68
|
+
|
69
|
+
class PostsController < ApplicationController
|
70
|
+
|
71
|
+
before_action :authenticate_user
|
72
|
+
|
73
|
+
before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
def index
|
80
|
+
|
81
|
+
@posts = Post.page(params[:page]).per(5).order(created_at: :desc)
|
82
|
+
|
83
|
+
@post = Post.find_by(id: params[:id])
|
84
|
+
|
85
|
+
if params[:image]
|
86
|
+
|
87
|
+
@post.image_name = "#{@post.id}.jpg"
|
88
|
+
|
89
|
+
image = params[:image]
|
90
|
+
|
91
|
+
File.binwrite("public/images_folder/#{@post.image_name}", image.read )
|
92
|
+
|
93
|
+
else
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
def show
|
106
|
+
|
107
|
+
@post = Post.find_by(id: params[:id])
|
108
|
+
|
109
|
+
@user = User.find_by(id: @post.user_id)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
if params[:image]
|
114
|
+
|
115
|
+
@post.image_name = "#{@post.id}.jpg"
|
116
|
+
|
117
|
+
image = params[:image]
|
118
|
+
|
119
|
+
File.binwrite("public/post_images/#{@post.image_name}", image.read)
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def new
|
130
|
+
|
131
|
+
@post = Post.new
|
132
|
+
|
133
|
+
if @current_user == nil
|
134
|
+
|
135
|
+
flash[:notice] = "ログインして下さい"
|
136
|
+
|
137
|
+
redirect_to("/login")
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
def create
|
148
|
+
|
149
|
+
@post = Post.new(
|
150
|
+
|
151
|
+
content: params[:content],
|
152
|
+
|
153
|
+
image_name: params[:image],
|
154
|
+
|
155
|
+
user_id: @current_user.id
|
156
|
+
|
157
|
+
)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
if params[:image]
|
162
|
+
|
163
|
+
@post.image_name = "#{@post.id}.jpg"
|
164
|
+
|
165
|
+
image = params[:image]
|
166
|
+
|
167
|
+
File.binwrite("public/images_folder/#{@post.image_name}", image.read)
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
if @post.save
|
174
|
+
|
175
|
+
redirect_to("/posts/index")
|
176
|
+
|
177
|
+
flash[:notice] = "投稿しました"
|
178
|
+
|
179
|
+
else
|
180
|
+
|
181
|
+
render("/posts/new")
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
def edit
|
194
|
+
|
195
|
+
@post = Post.find_by(id: params[:id])
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def update
|
204
|
+
|
205
|
+
@post = Post.find_by(id: params[:id])
|
206
|
+
|
207
|
+
@post.content = params[:content]
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
if params[:image]
|
212
|
+
|
213
|
+
@post.image_name = "#{@post.id}.jpg"
|
214
|
+
|
215
|
+
image = params[:image]
|
216
|
+
|
217
|
+
File.binwrite("public/post_images/#{@post.image_name}", image:read)
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if @post.save
|
224
|
+
|
225
|
+
redirect_to("/posts/index")
|
226
|
+
|
227
|
+
flash[:notice] = "投稿の編集をしました"
|
228
|
+
|
229
|
+
else
|
230
|
+
|
231
|
+
redirect_to("/posts/#{@post.id}/edit")
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
def destroy
|
242
|
+
|
243
|
+
@post = Post.find_by(id: params[:id])
|
244
|
+
|
245
|
+
@post.destroy
|
246
|
+
|
247
|
+
redirect_to("/posts/index")
|
248
|
+
|
249
|
+
flash[:notice] = "削除しました"
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
def ensure_correct_user
|
260
|
+
|
261
|
+
@post = Post.find_by(id: params[:id])
|
262
|
+
|
263
|
+
if @post.user_id != @current_user.id
|
264
|
+
|
265
|
+
flash[:notice] = "ユーザーが一致しません"
|
266
|
+
|
267
|
+
redirect_to("/posts/index")
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
private
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
def post_params
|
284
|
+
|
285
|
+
params.require(:post).permit(:title, :image)
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
### 該当のソースコード
|
300
|
+
|
301
|
+
**[upload.rb]**
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
class Upload < ApplicationRecord
|
306
|
+
|
307
|
+
mount_uploader :image, ImageUploader
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
### 該当のソースコード
|
316
|
+
|
317
|
+
[post.rb]
|
318
|
+
|
319
|
+
class Post < ApplicationRecord
|
320
|
+
|
321
|
+
mount_uploader :image, ImageUploader
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
### 該当のソースコード
|
330
|
+
|
331
|
+
[Gemfile]
|
332
|
+
|
333
|
+
source 'https://rubygems.org'
|
334
|
+
|
335
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
ruby '2.6.3'
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
gem 'rails-i18n'
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
|
354
|
+
|
355
|
+
gem 'rails', '~> 6.0.1'
|
356
|
+
|
357
|
+
# Use sqlite3 as the database for Active Record
|
358
|
+
|
359
|
+
gem 'sqlite3', '~> 1.4'
|
360
|
+
|
361
|
+
# Use Puma as the app server
|
362
|
+
|
363
|
+
gem 'puma', '~> 4.1'
|
364
|
+
|
365
|
+
# Use SCSS for stylesheets
|
366
|
+
|
367
|
+
gem 'sass-rails', '>= 6'
|
368
|
+
|
369
|
+
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
|
370
|
+
|
371
|
+
gem 'webpacker', '~> 4.0'
|
372
|
+
|
373
|
+
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
|
374
|
+
|
375
|
+
gem 'turbolinks', '~> 5'
|
376
|
+
|
377
|
+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
|
378
|
+
|
379
|
+
gem 'jbuilder', '~> 2.7'
|
380
|
+
|
381
|
+
# Use Redis adapter to run Action Cable in production
|
382
|
+
|
383
|
+
# gem 'redis', '~> 4.0'
|
384
|
+
|
385
|
+
# Use Active Model has_secure_password
|
386
|
+
|
387
|
+
# gem 'bcrypt', '~> 3.1.7'
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
# Use Active Storage variant
|
392
|
+
|
393
|
+
# gem 'image_processing', '~> 1.2'
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
# Reduces boot times through caching; required in config/boot.rb
|
398
|
+
|
399
|
+
gem 'bootsnap', '>= 1.4.2', require: false
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
group :development, :test do
|
404
|
+
|
405
|
+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
|
406
|
+
|
407
|
+
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
|
408
|
+
|
409
|
+
end
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
group :development do
|
414
|
+
|
415
|
+
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
|
416
|
+
|
417
|
+
gem 'web-console', '>= 3.3.0'
|
418
|
+
|
419
|
+
gem 'listen', '>= 3.0.5', '< 3.2'
|
420
|
+
|
421
|
+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
|
422
|
+
|
423
|
+
gem 'spring'
|
424
|
+
|
425
|
+
gem 'spring-watcher-listen', '~> 2.0.0'
|
426
|
+
|
427
|
+
end
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
group :test do
|
432
|
+
|
433
|
+
# Adds support for Capybara system testing and selenium driver
|
434
|
+
|
435
|
+
gem 'capybara', '>= 2.15'
|
436
|
+
|
437
|
+
gem 'selenium-webdriver'
|
438
|
+
|
439
|
+
# Easy installation and use of web drivers to run system tests with browsers
|
440
|
+
|
441
|
+
gem 'webdrivers'
|
442
|
+
|
443
|
+
end
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
448
|
+
|
449
|
+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
gem "refile", github: 'refile/refile', require: "refile/rails"
|
456
|
+
|
457
|
+
gem "refile-mini_magick", github: 'refile/refile-mini_magick'
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
gem 'kaminari'
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
gem 'carrierwave', '~> 2.0.2'
|
466
|
+
|
467
|
+
gem 'mini_magick'
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
|
474
|
+
|
65
475
|
```ここに言語名を入力
|
66
476
|
|
67
477
|
Ruby on Rails
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
Ruby on Railsにてポートフォリオの作成中です。投稿一覧ページにてTwitterや食べログのような投稿したテキスト+画像ファイルの表示をindexにて行いたい
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
ここに質問の内容を詳しく書いてください。
|
8
4
|
|
9
5
|
|
10
6
|
|
@@ -78,7 +74,7 @@
|
|
78
74
|
|
79
75
|
|
80
76
|
|
81
|
-
carrierwaveを使って試しましたが、変わらず。また画像ファイルの大きさも関係があるのかと思い、念のためCSSでスペースを多くとってみたりもしました。
|
77
|
+
carrierwaveを使って試しましたが、変わらず。また画像ファイルの大きさも関係があるのかと思い、念のためCSSでスペースを多くとってみたりもしました。初心者のためうまく説明ができず申し訳ありません。何かわかることがあったら教えていただけると幸いです。。。
|
82
78
|
|
83
79
|
|
84
80
|
|