teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを画像から変更しました。

2020/05/07 08:05

投稿

tedmosby
tedmosby

スコア46

title CHANGED
File without changes
body CHANGED
@@ -4,9 +4,59 @@
4
4
  ユーザ編集時には画像データをattachすることができるのですが、oauthから新規登録しようとするときのattachがうまく行かず詰まっています。
5
5
  具体的なコードは以下です。
6
6
 
7
+ ```userrb
8
+ def self.find_for_oauth(auth)
7
- ![user.rb](23d1d80a197b6f24067c0aeedf1209ea.png)
9
+ user = User.where(uid: auth.uid, provider: auth.provider).first
8
- ここでの30~31行目がattachをしようとしてうまく行かずに詰まっている部分です。
9
10
 
11
+ user ||= User.create(
12
+ uid: auth.uid,
13
+ provider: auth.provider,
14
+ name: auth.info.name,
15
+ profile: set_profile(auth),
16
+ email: set_email(auth),
10
- ![application_controller.rb](d47b857193c2f5f592366166c6ff094d.png)
17
+ password: Devise.friendly_token[0, 20]
18
+ )
19
+ user.image.attach(auth.info.image)
20
+ user
21
+ end
11
22
 
23
+ class << self
24
+ private
25
+
26
+ def set_email(auth)
27
+ auth.info.email || "#{auth.uid}-#{auth.provider}@example.com"
28
+ end
29
+
30
+ def set_profile(auth)
31
+ auth.info.description
32
+ end
33
+ end
34
+ ```
35
+ ここでのuser.image.attach(auth.info.image)の部分が、attachをしようとしてうまく行かずに詰まっている部分です。
36
+
37
+ ```applicationcontroller
38
+ # frozen_string_literal: true
39
+
40
+ class ApplicationController < ActionController::Base
41
+ before_action :configure_permitted_parameters, if: :devise_controller?
42
+
43
+ def after_sign_in_path_for(resource)
44
+ user_path(resource)
45
+ end
46
+
47
+ def after_sign_out_path_for(_resource)
48
+ root_path
49
+ end
50
+
51
+ def configure_permitted_parameters
52
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
53
+ devise_parameter_sanitizer.permit(:account_update, keys: %i[name profile image])
54
+ end
55
+
56
+ def search_param
57
+ params.permit(:search)
58
+ end
59
+ end
60
+
61
+ ```
12
62
  わかる方がいらっしゃれば、教えていただけるとありがたいです。