質問編集履歴
2
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -37,3 +37,145 @@
|
|
37
37
|
SQLite3
|
38
38
|
|
39
39
|
Rails
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
**app/controllers/users_controller.rb**
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
class UsersController < ApplicationController
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def show
|
52
|
+
|
53
|
+
@user = User.find(params[:id])
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def new
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
**app/models/user.rb**
|
72
|
+
|
73
|
+
```
|
74
|
+
|
75
|
+
class User < ApplicationRecord
|
76
|
+
|
77
|
+
before_save { self.email = email.downcase! }
|
78
|
+
|
79
|
+
validates :name, presence: true, length: { maximum: 50 }
|
80
|
+
|
81
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i
|
82
|
+
|
83
|
+
validates :email, presence: true, length: { maximum: 255 },
|
84
|
+
|
85
|
+
format: { with: VALID_EMAIL_REGEX },
|
86
|
+
|
87
|
+
uniqueness: { case_sensitive: false }
|
88
|
+
|
89
|
+
has_secure_password
|
90
|
+
|
91
|
+
validates :password, presence: true, length: { minimum: 6 }
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
**db/migrate/20210209213647_create_users**
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
class CreateUsers < ActiveRecord::Migration[5.1]
|
104
|
+
|
105
|
+
def change
|
106
|
+
|
107
|
+
create_table :users do |t|
|
108
|
+
|
109
|
+
t.string :name
|
110
|
+
|
111
|
+
t.string :email
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
t.timestamps
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
**db/migrate/20210210225051_add_index_to_users_email**
|
126
|
+
|
127
|
+
```ここに言語を入力
|
128
|
+
|
129
|
+
class AddIndexToUsersEmail < ActiveRecord::Migration[5.1]
|
130
|
+
|
131
|
+
def change
|
132
|
+
|
133
|
+
add_index :users, :email, unique: true
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
**db/migrate/20210210233322_add_password_digest_to_users**
|
142
|
+
|
143
|
+
```ここに言語を入力
|
144
|
+
|
145
|
+
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.1]
|
146
|
+
|
147
|
+
def change
|
148
|
+
|
149
|
+
add_column :users, :password_digest, :string
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
**helpers/users_helper.rb**
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
module UsersHelper
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
# Gravatar読み込み
|
168
|
+
|
169
|
+
def gravatar_for(user)
|
170
|
+
|
171
|
+
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
|
172
|
+
|
173
|
+
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
|
174
|
+
|
175
|
+
image_tag(gravatar_url, alt: user.name, class: "gravatar")
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
```
|
1
詳しい状況を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,9 @@
|
|
10
10
|
|
11
11
|
user.update_attributeなどでも試しましたが、同じ結果でした。
|
12
12
|
|
13
|
+
User.newの時まではuser.emailとしてemailの値を取り出せるのですがuser.saveで保存後にuser.emailとするとnilになります。
|
14
|
+
|
13
|
-
エラーなども表示されず困っています。考えられる原因などでも良いので
|
15
|
+
エラーなども表示されず困っています。考えられる原因などでも良いのでご教授頂けると幸いです。回答お願い致します。
|
14
16
|
|
15
17
|
|
16
18
|
|