質問編集履歴
2
ソースコードを変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -170,6 +170,164 @@
|
|
170
170
|
|
171
171
|
|
172
172
|
|
173
|
+
■2017/8/13更新
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
■ログイン用コントローラー
|
178
|
+
|
179
|
+
```ここに言語を入力
|
180
|
+
|
181
|
+
class LoginController < ApplicationController
|
182
|
+
|
183
|
+
layout 'info_get'
|
184
|
+
|
185
|
+
protect_from_forgery
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
def index
|
190
|
+
|
191
|
+
@title = 'トップ'
|
192
|
+
|
193
|
+
@err_msg = Hash.new
|
194
|
+
|
195
|
+
@user = User.new
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
def login
|
202
|
+
|
203
|
+
user = User.new user_params
|
204
|
+
|
205
|
+
if user.valid?(:login) then
|
206
|
+
|
207
|
+
set_user = User.find_by(email: user.email)
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
unless set_user.authenticate(user.password) then
|
212
|
+
|
213
|
+
return redirect_to '/login'
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
if params[:auto_login] then
|
220
|
+
|
221
|
+
auto_login = AutoLogin.new
|
222
|
+
|
223
|
+
auto_login.save(:user_id => set_user.id)
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
Rails.cache.write('login_user', set_user)
|
230
|
+
|
231
|
+
return redirect_to '/home'
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
#エラーメッセージの連想配列を取得
|
238
|
+
|
239
|
+
@err_msg = User.error_message(user)
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
def ajax
|
248
|
+
|
249
|
+
f = File.open("#{Rails.root}/app/assets/javascripts/carousel.xml")
|
250
|
+
|
251
|
+
render :xml => f
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
def destroy
|
258
|
+
|
259
|
+
Rails.cache.delete('login_user')
|
260
|
+
|
261
|
+
redirect_to '/login'
|
262
|
+
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
private
|
268
|
+
|
269
|
+
def user_params
|
270
|
+
|
271
|
+
params.require(:user).permit(:email, :password, :auto_login)
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
```
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
■login/login.js.erb
|
284
|
+
|
285
|
+
```ここに言語を入力
|
286
|
+
|
287
|
+
$('#js-valid_email').text('<%= @err_msg[:email] %>');
|
288
|
+
|
289
|
+
$('#js-valid_password').text('<%= @err_msg[:password] %>');
|
290
|
+
|
291
|
+
```
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
■login/index.js.erb
|
296
|
+
|
297
|
+
```ここに言語を入力
|
298
|
+
|
299
|
+
<%= form_for(@user, url:{controller: 'login', action: 'login'}, html:{id: 'js-form', class: 'form form__login'}, remote:true) do |f| %>
|
300
|
+
|
301
|
+
<fieldset class="form__field--login">
|
302
|
+
|
303
|
+
<%= f.label :email, 'Email', :class => 'form__label' %>
|
304
|
+
|
305
|
+
<%= f.text_field :email, :id => 'user_email', :class => 'form__text' , :placeholder => 'Email' %>
|
306
|
+
|
307
|
+
<span class="form__helper" id="js-valid_email"></span>
|
308
|
+
|
309
|
+
<%= f.label :password, 'パスワード', :class => 'form__label' %>
|
310
|
+
|
311
|
+
<%= f.password_field :password, :id => 'user_password', :class => 'form__password' , :placeholder => 'パスワード' %>
|
312
|
+
|
313
|
+
<span class="form__helper" id="js-valid_password"></span>
|
314
|
+
|
315
|
+
<label for="auto_login" class="form__checkboxLbl"><input type="checkbox" name="auto_login" id="auto_login" class="form__checkbox">
|
316
|
+
|
317
|
+
次回から自動ログインする
|
318
|
+
|
319
|
+
</label>
|
320
|
+
|
321
|
+
</fieldset>
|
322
|
+
|
323
|
+
<%= f.submit 'ログイン', {:class => 'btn btn__submit--size_large', :id => 'js-submitBtn'} %>
|
324
|
+
|
325
|
+
<% end %>
|
326
|
+
|
327
|
+
```
|
328
|
+
|
329
|
+
|
330
|
+
|
173
331
|
|
174
332
|
|
175
333
|
■動作環境
|
1
ソースコードを最新に更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,13 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
+
■ブラウザに表示されるjson形式のメッセージ
|
20
|
+
|
21
|
+
{"email":"Emailを入力して下さい。","password":"パスワードを入力して下さい。"}
|
22
|
+
|
23
|
+
|
24
|
+
|
19
|
-
■モーダルウィンドウのview
|
25
|
+
■モーダルウィンドウのview(login/index.html.erb)
|
20
26
|
|
21
27
|
```ここに言語を入力
|
22
28
|
|
@@ -24,29 +30,29 @@
|
|
24
30
|
|
25
31
|
<fieldset class="form__field--login">
|
26
32
|
|
27
|
-
|
33
|
+
<%= f.label :email, 'Email', :class => 'form__label' %>
|
28
34
|
|
29
|
-
|
35
|
+
<%= f.text_field :email, :id => 'user_email', :class => 'form__text' , :placeholder => 'Email' %>
|
30
36
|
|
31
|
-
|
37
|
+
<span class="form__helper" id="js-valid_email"></span>
|
32
38
|
|
33
|
-
|
39
|
+
<%= f.label :password, 'パスワード', :class => 'form__label' %>
|
34
40
|
|
35
|
-
|
41
|
+
<%= f.password_field :password, :id => 'user_password', :class => 'form__password' , :placeholder => 'パスワード' %>
|
36
42
|
|
37
|
-
|
43
|
+
<span class="form__helper" id="js-valid_password"></span>
|
38
44
|
|
39
|
-
|
45
|
+
<label for="auto_login" class="form__checkboxLbl"><input type="checkbox" name="auto_login" id="auto_login" class="form__checkbox">
|
40
46
|
|
41
|
-
|
47
|
+
次回から自動ログインする
|
42
48
|
|
43
|
-
|
49
|
+
</label>
|
44
50
|
|
45
51
|
</fieldset>
|
46
52
|
|
47
53
|
<%= f.submit 'ログイン', {:class => 'btn btn__submit--size_large', :id => 'js-submitBtn'} %>
|
48
54
|
|
49
|
-
<% end %>
|
55
|
+
<% end %>
|
50
56
|
|
51
57
|
```
|
52
58
|
|
@@ -54,7 +60,7 @@
|
|
54
60
|
|
55
61
|
■ログイン用のコントローラー
|
56
62
|
|
57
|
-
return @errMsg.to_json
|
63
|
+
return render :json => @errMsg.to_jsonとなっている箇所が不具合が発生しているところになります。
|
58
64
|
|
59
65
|
|
60
66
|
|
@@ -78,17 +84,15 @@
|
|
78
84
|
|
79
85
|
@user = User.new user_params
|
80
86
|
|
87
|
+
unless @user.valid?(:login) then
|
81
88
|
|
89
|
+
#エラーメッセージの連想配列を取得
|
82
90
|
|
83
|
-
|
91
|
+
@errMsg = User.error_message(@user)
|
84
92
|
|
85
|
-
@errMsg = set_errmsg
|
86
|
-
|
87
|
-
return @errMsg.to_json
|
93
|
+
return render :json => @errMsg.to_json
|
88
94
|
|
89
95
|
end
|
90
|
-
|
91
|
-
|
92
96
|
|
93
97
|
set_user = User.find_by(email: @user.email)
|
94
98
|
|
@@ -104,13 +108,23 @@
|
|
104
108
|
|
105
109
|
Rails.cache.write('login_user', set_user)
|
106
110
|
|
107
|
-
redirect_to '/home'
|
111
|
+
return redirect_to '/home'
|
108
112
|
|
109
113
|
else
|
110
114
|
|
111
115
|
@user = User.new
|
112
116
|
|
113
117
|
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
def destroy
|
124
|
+
|
125
|
+
Rails.cache.delete('login_user')
|
126
|
+
|
127
|
+
redirect_to '/login'
|
114
128
|
|
115
129
|
end
|
116
130
|
|
@@ -124,35 +138,37 @@
|
|
124
138
|
|
125
139
|
end
|
126
140
|
|
127
|
-
|
128
|
-
|
129
|
-
def set_errmsg
|
130
|
-
|
131
|
-
errMsg = Hash.new
|
132
|
-
|
133
|
-
@user.errors.messages.each do |key, val|
|
134
|
-
|
135
|
-
if key == :password_confirmation && !errMsg.has_key?(:password) then
|
136
|
-
|
137
|
-
errMsg[:password] = val[0]
|
138
|
-
|
139
|
-
next
|
140
|
-
|
141
|
-
|
141
|
+
end
|
142
|
-
|
143
|
-
errMsg[key] = val[0]
|
144
|
-
|
145
|
-
end
|
146
142
|
|
147
143
|
|
148
144
|
|
149
|
-
|
145
|
+
```
|
150
146
|
|
151
|
-
|
147
|
+
■view側のjavascript(login/index.html.erb)
|
152
148
|
|
149
|
+
```ここに言語を入力
|
150
|
+
|
151
|
+
$(function () {
|
152
|
+
|
153
|
+
$('#js-form').on('ajax:success',function(data){
|
154
|
+
|
155
|
+
console.log(data);
|
156
|
+
|
153
|
-
|
157
|
+
});
|
158
|
+
|
159
|
+
$('#js-form').on('ajax:error',function(data){
|
160
|
+
|
161
|
+
console.log(data);
|
162
|
+
|
163
|
+
});
|
164
|
+
|
165
|
+
});
|
166
|
+
|
167
|
+
</script>
|
154
168
|
|
155
169
|
```
|
170
|
+
|
171
|
+
|
156
172
|
|
157
173
|
|
158
174
|
|