質問編集履歴
6
説明追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -165,6 +165,57 @@
|
|
165
165
|
|
166
166
|
|
167
167
|
```
|
168
|
+
application_controller
|
169
|
+
|
170
|
+
class ApplicationController < ActionController::Base
|
171
|
+
before_action :set_current_user
|
172
|
+
|
173
|
+
|
174
|
+
def top
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
def set_current_user
|
179
|
+
@current_user = User.find_by(id: session[:user_id])
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
def authenticate_user
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
def forbid_login_user
|
188
|
+
if @current_user
|
189
|
+
flash[:notice] = "すでにログインしています"
|
190
|
+
redirect_to("/posts/index")
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
|
200
|
+
```
|
201
|
+
home_controller
|
202
|
+
|
203
|
+
class HomeController < ApplicationController
|
204
|
+
before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
def top
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
```
|
216
|
+
|
217
|
+
|
218
|
+
```
|
168
219
|
users/login_form.html.erb
|
169
220
|
|
170
221
|
<div class = "form-error">
|
5
説明追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -257,8 +257,45 @@
|
|
257
257
|
end
|
258
258
|
```
|
259
259
|
|
260
|
+
```
|
261
|
+
user/edit
|
260
262
|
|
263
|
+
<div class="user-index">
|
264
|
+
<h1>ユーザー情報の編集</h1>
|
265
|
+
<% @user.errors.full_messages.each do |message| %>
|
266
|
+
<div class="form-error">
|
267
|
+
<%= message %>
|
268
|
+
</div>
|
269
|
+
<% end %>
|
261
270
|
|
271
|
+
|
272
|
+
<div class="user-edit-container">
|
273
|
+
<%= form_for @user do |f| %>
|
274
|
+
|
275
|
+
<div class="user-edit-item">
|
276
|
+
<%= f.label :name, "ユーザー名(※必須)", class: "form" %><br>
|
277
|
+
<%= f.text_field :name %><br>
|
278
|
+
|
279
|
+
<%= f.label :image, "画像", class: "form" %><br>
|
280
|
+
<%= f.file_field :image %><br>
|
281
|
+
|
282
|
+
<%= f.label :email, "メールアドレス(※必須)", class: "form" %><br>
|
283
|
+
<%= f.text_field :email %><br>
|
284
|
+
|
285
|
+
<%= f.label :password, "パスワード(※必須)", class: "form" %><br>
|
286
|
+
<%= f.text_field :password %><br>
|
287
|
+
|
288
|
+
<%= f.submit "新規登録", class: "form" %>
|
289
|
+
|
290
|
+
</div>
|
291
|
+
<% end %>
|
292
|
+
</div>
|
293
|
+
</div>
|
294
|
+
|
295
|
+
```
|
296
|
+
|
297
|
+
|
298
|
+
|
262
299
|
自分で試したこととしては、form_forの@userがnilなので、login_formアクション内で@userオブジェクトを生成したりもしたのですが、特に変わらず、、
|
263
300
|
(具体的には、
|
264
301
|
・@user = User.find_by(email: params[:email], password: params[:password])
|
4
説明追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -227,10 +227,10 @@
|
|
227
227
|
create_table "users", force: :cascade do |t|
|
228
228
|
t.string "name"
|
229
229
|
t.string "email"
|
230
|
-
t.string "password"
|
231
230
|
t.string "image"
|
232
231
|
t.datetime "created_at", precision: 6, null: false
|
233
232
|
t.datetime "updated_at", precision: 6, null: false
|
233
|
+
t.string "password_digest"
|
234
234
|
end
|
235
235
|
|
236
236
|
end
|
3
説明追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -103,17 +103,18 @@
|
|
103
103
|
|
104
104
|
|
105
105
|
def login
|
106
|
-
@user = User.find_by(email: params[:email]
|
106
|
+
@user = User.find_by(email: params[:email])
|
107
|
+
|
107
|
-
|
108
|
+
if @user && @user.authenticate(params[:password])
|
108
|
-
|
109
|
+
session[:user_id] = @user.id
|
109
|
-
|
110
|
+
flash[:notice] = "ログイン成功"
|
110
|
-
|
111
|
+
redirect_to("/posts/index")
|
111
|
-
|
112
|
+
else
|
112
|
-
|
113
|
+
@error_message = "メールアドレスかパスワードが違います"
|
113
|
-
|
114
|
+
@email = params[:email]
|
114
|
-
|
115
|
+
@password = params[:password]
|
115
|
-
|
116
|
+
render("users/login_form")
|
116
|
-
|
117
|
+
end
|
117
118
|
end
|
118
119
|
|
119
120
|
|
@@ -236,8 +237,28 @@
|
|
236
237
|
|
237
238
|
```
|
238
239
|
|
240
|
+
```
|
241
|
+
models/User
|
239
242
|
|
243
|
+
class User < ApplicationRecord
|
244
|
+
has_secure_password
|
245
|
+
mount_uploader :image,ImageUploader
|
246
|
+
validates :name, {presence: true}
|
247
|
+
validates :email, {presence: true}
|
248
|
+
validates :password, {presence: true}
|
240
249
|
|
250
|
+
|
251
|
+
def posts
|
252
|
+
return Post.where(user_id: self.id)
|
253
|
+
end
|
254
|
+
|
255
|
+
paginates_per 8
|
256
|
+
|
257
|
+
end
|
258
|
+
```
|
259
|
+
|
260
|
+
|
261
|
+
|
241
262
|
自分で試したこととしては、form_forの@userがnilなので、login_formアクション内で@userオブジェクトを生成したりもしたのですが、特に変わらず、、
|
242
263
|
(具体的には、
|
243
264
|
・@user = User.find_by(email: params[:email], password: params[:password])
|
2
説明追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
ログインフォームをform_forで作成できず、困っています
|
body
CHANGED
File without changes
|
1
説明追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -239,5 +239,14 @@
|
|
239
239
|
|
240
240
|
|
241
241
|
自分で試したこととしては、form_forの@userがnilなので、login_formアクション内で@userオブジェクトを生成したりもしたのですが、特に変わらず、、
|
242
|
+
(具体的には、
|
243
|
+
・@user = User.find_by(email: params[:email], password: params[:password])
|
244
|
+
・@user = User.find_by(id: params[:id])
|
245
|
+
など。)
|
242
246
|
|
247
|
+
sessionコントローラーなどは用いずに実装したいと考えています。(できないのであれば仕方ないとは考えています)。
|
248
|
+
また、form_tagで実装すると機能するので、form_tagのままでいいかなとも思いつつ、全体としてform_forで揃えるべきだよな、、とも考えています。
|
249
|
+
|
250
|
+
|
251
|
+
|
243
|
-
もしお分かりの方がいらっしゃいましたら、アドバイスいただけると幸いです。
|
252
|
+
login_formアクション内で@userオブジェクトに何を定義すればいいのでしょうか?もしお分かりの方がいらっしゃいましたら、アドバイスいただけると幸いです。
|