質問編集履歴

3

omniauth.rb などを加筆

2020/03/17 09:58

投稿

jime
jime

スコア10

test CHANGED
File without changes
test CHANGED
@@ -624,6 +624,68 @@
624
624
 
625
625
 
626
626
 
627
+ ```
628
+
629
+ **secrets.yml**
630
+
631
+
632
+
633
+ default_twitter: &default_twitter
634
+
635
+ twitter_api_key: <%= ENV["TWITTER_CONSUMER_KEY"] %>
636
+
637
+ twitter_api_secret: <%= ENV["TWITTER_CONSUMER_SECRET"] %>
638
+
639
+
640
+
641
+
642
+
643
+
644
+
645
+ development:
646
+
647
+ secret_key_base:
648
+
649
+ <<: *default_twitter
650
+
651
+
652
+
653
+ test:
654
+
655
+ secret_key_base:
656
+
657
+ <<: *default_twitter
658
+
659
+
660
+
661
+ production:
662
+
663
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
664
+
665
+ twitter_api_key: <%= ENV["TWITTER_CONSUMER_KEY"] %>
666
+
667
+ twitter_api_secret: <%= ENV["TWITTER_CONSUMER_SECRET"] %>
668
+
669
+ ```
670
+
671
+
672
+
673
+ ```
674
+
675
+ **omniauth.rb**
676
+
677
+
678
+
679
+ Rails.application.config.middleware.use OmniAuth::Builder do
680
+
681
+ provider :twitter, ENV['TWITTER_CONSUMER_KEY'], ENV['TWITTER_CONSUMER_SECRET'], callback_url: "https://https://pile-up.herokuapp.com//users/auth/twitter/callback"
682
+
683
+ end
684
+
685
+ ```
686
+
687
+
688
+
627
689
  ### 試したこと
628
690
 
629
691
 

2

controllerとviewの加筆

2020/03/17 09:58

投稿

jime
jime

スコア10

test CHANGED
File without changes
test CHANGED
@@ -320,6 +320,310 @@
320
320
 
321
321
 
322
322
 
323
+ ```
324
+
325
+ **controllers/users_controller.rb**
326
+
327
+
328
+
329
+ class UsersController < ApplicationController
330
+
331
+
332
+
333
+ before_action :authenticate_user,{only:[:index,:show,:edit,:update]}
334
+
335
+
336
+
337
+ before_action :forbid_login_user,{only:[:new,:create,:login_form,:login]}
338
+
339
+
340
+
341
+ before_action :ensure_correct_user,{only:[:edit,:update]}
342
+
343
+
344
+
345
+ def show
346
+
347
+ @user = User.find_by(id: params[:id])
348
+
349
+ @post = Post.where(user_id: @user.id)
350
+
351
+
352
+
353
+ end
354
+
355
+
356
+
357
+ def new
358
+
359
+ @user = User.new
360
+
361
+ end
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+ def create
370
+
371
+ @user = User.new(
372
+
373
+ name: params[:name],
374
+
375
+ email: params[:email],
376
+
377
+ image_name: "pile-up3.jpg",
378
+
379
+ password: params[:password]
380
+
381
+ )
382
+
383
+ if @user.save
384
+
385
+ session[:user_id] = @user.id
386
+
387
+ redirect_to("/users/#{@user.id}")
388
+
389
+ else
390
+
391
+ render("users/new")
392
+
393
+ end
394
+
395
+
396
+
397
+ end
398
+
399
+
400
+
401
+
402
+
403
+ #twitter認証の
404
+
405
+ def creates
406
+
407
+
408
+
409
+ @user = User.new(
410
+
411
+ name: params[:name],
412
+
413
+ email: params[:email],
414
+
415
+ image_name: "pile-up3.jpg",
416
+
417
+ password: params[:password]
418
+
419
+ )
420
+
421
+
422
+
423
+ user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])#request.env['omniauth.auth']はTwitter認証で得た情報を格納するもの
424
+
425
+ if user
426
+
427
+ session[:user_id] = user.id
428
+
429
+ redirect_to("/users/#{user.id}")
430
+
431
+ else
432
+
433
+ redirect_to root_path
434
+
435
+ end
436
+
437
+
438
+
439
+ end
440
+
441
+ #twitter認証の
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+ def edit
454
+
455
+ @user = User.find_by(id: params[:id])
456
+
457
+ end
458
+
459
+
460
+
461
+ def update
462
+
463
+ @user = User.find_by(id: params[:id])
464
+
465
+ @user.name = params[:name]
466
+
467
+ @user.email = params[:email]
468
+
469
+
470
+
471
+ if params[:image]
472
+
473
+ @user.image_name = "#{@user.id}.jpg"
474
+
475
+ image = params[:image]
476
+
477
+ File.binwrite("public/user_images/#{@user.image_name}",image.read)
478
+
479
+ end
480
+
481
+
482
+
483
+ if @user.save
484
+
485
+ redirect_to("/users/#{@user.id}")
486
+
487
+ else
488
+
489
+ render("users/edit")
490
+
491
+ end
492
+
493
+
494
+
495
+ end
496
+
497
+
498
+
499
+ def login_form
500
+
501
+
502
+
503
+
504
+
505
+ end
506
+
507
+
508
+
509
+ def login
510
+
511
+ @user = User.find_by(email: params[:email])
512
+
513
+ if @user && @user.authenticate(params[:password])
514
+
515
+ session[:user_id] = @user.id
516
+
517
+ redirect_to("/users/#{@user.id}")
518
+
519
+ else
520
+
521
+ @error_message = "メールアドレスまたはパスワードが間違っています"
522
+
523
+ @email = params[:email]
524
+
525
+ @password = params[:password]
526
+
527
+ render("users/login_form")
528
+
529
+ end
530
+
531
+ end
532
+
533
+
534
+
535
+ def logout
536
+
537
+ session[:user_id] = nil
538
+
539
+ redirect_to("/")
540
+
541
+ end
542
+
543
+
544
+
545
+
546
+
547
+ def ensure_correct_user
548
+
549
+ if @current_user.id != params[:id].to_i
550
+
551
+ flash[:notice] = "権限がありません"
552
+
553
+ redirect_to("/posts/index")
554
+
555
+ end
556
+
557
+ end
558
+
559
+ end
560
+
561
+
562
+
563
+ ```
564
+
565
+
566
+
567
+ ```
568
+
569
+ **posts/index.html.erb**
570
+
571
+
572
+
573
+
574
+
575
+ <div class="main-wrapper">
576
+
577
+ <div class="cont">
578
+
579
+ <div class="main-wrapper-main">
580
+
581
+ <div class="main-title">
582
+
583
+ <h1>ピラプ</h1>
584
+
585
+ <p>あなたの頑張りを可視化するアプリケーション</p>
586
+
587
+ </div>
588
+
589
+ <div class="main-btn">
590
+
591
+
592
+
593
+
594
+
595
+ <% if @current_user %>
596
+
597
+ <a class="main-btn1" href="/posts/new" >新規投稿</a>
598
+
599
+ <a class="main-btn2" href="/users/<%= @current_user.id %>" >マイページ</a>
600
+
601
+
602
+
603
+ <% else %>
604
+
605
+ <a class="main-btn1" href="/signup" >新規登録はこちらから</a>
606
+
607
+ <a class="main-btn2" href="/login" >メールアドレスでログイン</a>
608
+
609
+ <a class="main-btn3" href="/auth/twitter" >Twitterでログイン</a>
610
+
611
+ <% end %>
612
+
613
+
614
+
615
+ </div>
616
+
617
+ </div>
618
+
619
+ </div>
620
+
621
+ </div>
622
+
623
+ ```
624
+
625
+
626
+
323
627
  ### 試したこと
324
628
 
325
629
 

1

gem とrails のバージョンを加筆

2020/03/17 09:54

投稿

jime
jime

スコア10

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,10 @@
30
30
 
31
31
 
32
32
 
33
+ Rubyのバージョンは2.5.0、Railsのバージョンは5.2.4.1です
34
+
35
+
36
+
33
37
  ### 発生している問題・エラーメッセージ
34
38
 
35
39
  ```
@@ -130,20 +134,12 @@
130
134
 
131
135
 
132
136
 
133
-
134
-
135
-
136
-
137
137
  get 'auth/:provider/callback' => 'users#creates'#このpathを通して認証が行われる。
138
138
 
139
139
 
140
140
 
141
141
 
142
142
 
143
-
144
-
145
-
146
-
147
143
  end
148
144
 
149
145
 
@@ -152,6 +148,178 @@
152
148
 
153
149
 
154
150
 
151
+ ```
152
+
153
+ **Gemfile**
154
+
155
+
156
+
157
+ source 'https://rubygems.org'
158
+
159
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
160
+
161
+
162
+
163
+ ruby '2.5.0'
164
+
165
+
166
+
167
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
168
+
169
+ gem 'rails', '~> 5.2.4'
170
+
171
+ # Use sqlite3 as the database for Active Record
172
+
173
+ gem 'sqlite3', group: :development
174
+
175
+ gem 'pg', group: :production
176
+
177
+ # Use Puma as the app server
178
+
179
+ gem 'puma', '~> 3.12'
180
+
181
+ # Use SCSS for stylesheets
182
+
183
+ gem 'sass-rails', '~> 5.0'
184
+
185
+ # Use Uglifier as compressor for JavaScript assets
186
+
187
+ gem 'uglifier', '>= 1.3.0'
188
+
189
+ # See https://github.com/rails/execjs#readme for more supported runtimes
190
+
191
+ # gem 'mini_racer', platforms: :ruby
192
+
193
+
194
+
195
+ # Use CoffeeScript for .coffee assets and views
196
+
197
+ gem 'coffee-rails', '~> 4.2'
198
+
199
+ # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
200
+
201
+ gem 'turbolinks', '~> 5'
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+ gem 'bcrypt'
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+ gem 'omniauth'
220
+
221
+ gem 'omniauth-twitter'
222
+
223
+ gem 'dotenv-rails'
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
234
+
235
+ gem 'jbuilder', '~> 2.5'
236
+
237
+ # Use Redis adapter to run Action Cable in production
238
+
239
+ # gem 'redis', '~> 4.0'
240
+
241
+ # Use ActiveModel has_secure_password
242
+
243
+ # gem 'bcrypt', '~> 3.1.7'
244
+
245
+
246
+
247
+ # Use ActiveStorage variant
248
+
249
+ # gem 'mini_magick', '~> 4.8'
250
+
251
+
252
+
253
+ # Use Capistrano for deployment
254
+
255
+ # gem 'capistrano-rails', group: :development
256
+
257
+
258
+
259
+ # Reduces boot times through caching; required in config/boot.rb
260
+
261
+ gem 'bootsnap', '>= 1.1.0', require: false
262
+
263
+
264
+
265
+ group :development, :test do
266
+
267
+ # Call 'byebug' anywhere in the code to stop execution and get a debugger console
268
+
269
+ gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
270
+
271
+ gem 'pry-rails'
272
+
273
+ end
274
+
275
+
276
+
277
+ group :development do
278
+
279
+ # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
280
+
281
+ gem 'web-console', '>= 3.3.0'
282
+
283
+ gem 'listen', '>= 3.0.5', '< 3.2'
284
+
285
+ # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
286
+
287
+ gem 'spring'
288
+
289
+ gem 'spring-watcher-listen', '~> 2.0.0'
290
+
291
+ end
292
+
293
+
294
+
295
+ group :test do
296
+
297
+ # Adds support for Capybara system testing and selenium driver
298
+
299
+ gem 'capybara', '>= 2.15'
300
+
301
+ gem 'selenium-webdriver'
302
+
303
+ # Easy installation and use of chromedriver to run system tests with Chrome
304
+
305
+ gem 'chromedriver-helper'
306
+
307
+ end
308
+
309
+
310
+
311
+ # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
312
+
313
+ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
314
+
315
+
316
+
317
+
318
+
319
+ ```
320
+
321
+
322
+
155
323
  ### 試したこと
156
324
 
157
325