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

質問編集履歴

2

文言修正

2017/07/25 14:14

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,9 +5,6 @@
5
5
  ユーザ名、Email、パスワードなどがDB上に反映されません。
6
6
  sqlite3で検索をかけてみると「ID||||||作成日時|更新日時」のように表示されます。
7
7
 
8
-
9
- 2017/7/25追記
10
- 下記の修正を施しました。
11
8
  ```ここに言語を入力
12
9
  class SignupController < ApplicationController
13
10
  layout 'info_get'
@@ -117,6 +114,11 @@
117
114
 
118
115
  どのように対処したらよいのでしょうか。
119
116
 
117
+
118
+ -------------------------------------
119
+ 2017/7/25追記
120
+ 下記の修正を施しました。
121
+
120
122
  ■修正後のコントローラー
121
123
  ```ここに言語を入力
122
124
  class SignupController < ApplicationController

1

回答頂いた内容を踏まえてコントローラーを修正、userモデルのコード追加

2017/07/25 14:14

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,9 @@
5
5
  ユーザ名、Email、パスワードなどがDB上に反映されません。
6
6
  sqlite3で検索をかけてみると「ID||||||作成日時|更新日時」のように表示されます。
7
7
 
8
+
9
+ 2017/7/25追記
10
+ 下記の修正を施しました。
8
11
  ```ここに言語を入力
9
12
  class SignupController < ApplicationController
10
13
  layout 'info_get'
@@ -112,4 +115,95 @@
112
115
  また、セッション管理の為にmemchachedを使用しています。
113
116
 
114
117
 
115
- どのように対処したらよいのでしょうか。
118
+ どのように対処したらよいのでしょうか。
119
+
120
+ ■修正後のコントローラー
121
+ ```ここに言語を入力
122
+ class SignupController < ApplicationController
123
+ layout 'info_get'
124
+ protect_from_forgery
125
+
126
+ include SessionManagement
127
+ before_action :is_session, only: [:index, :confirm]
128
+ before_action :sign_in!, only: [:complete]
129
+
130
+ def index
131
+ @title = 'ユーザー情報入力'
132
+ @errMsg = Hash.new
133
+
134
+ if request.get? then
135
+ @user = User.new
136
+ if Rails.cache.exist?('signup_user') then
137
+ signup_user = Rails.cache.read('signup_user')
138
+
139
+ @user.name = signup_user.name
140
+ @user.email = signup_user.email
141
+ end
142
+ else
143
+ @user = User.new user_params
144
+
145
+ if @user.valid? then
146
+ Rails.cache.write('signup_user', @user)
147
+ redirect_to '/signup/confirm'
148
+ else
149
+ #エラーメッセージの連想配列を生成
150
+ @user.errors.messages.each do |key, val|
151
+ if key == :password_confirmation && !@errMsg.has_key?(:password) then
152
+ @errMsg[:password] = val[0]
153
+ next
154
+ end
155
+ @errMsg[key] = val[0]
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ def confirm
162
+ @title = 'ユーザー情報入力確認'
163
+
164
+ if request.post? then
165
+ @user = Rails.cache.read('signup_user')
166
+ if @user.save(validate: false) then
167
+ login_user = User.find_by(email: @user.email)
168
+
169
+ if login_user && login_user.authenticate(@user.password) then
170
+ Rails.cache.delete('signup_user')
171
+ Rails.cache.write('login_user', login_user)
172
+ redirect_to '/signup/complete'
173
+ end
174
+
175
+ end
176
+ redirect_to '/signup/confirm'
177
+ else
178
+ @user = Rails.cache.read('signup_user')
179
+ end
180
+ end
181
+
182
+ def complete
183
+ @title = 'ユーザー情報登録完了'
184
+ end
185
+
186
+ private
187
+ def user_params
188
+ params.require(:user).permit(:name, :email, :password, :password_confirmation)
189
+ end
190
+
191
+ end
192
+
193
+ ```
194
+
195
+ ■userモデル
196
+ ```ここに言語を入力
197
+ class User < ApplicationRecord
198
+ include ActiveModel::Model
199
+
200
+ has_secure_password validations: false
201
+
202
+ attr_accessor :name, :email, :password, :password_confirmation
203
+
204
+ validates :name, presence: {message: 'ユーザー名を入力して下さい。'}
205
+ validates :email, presence: {message: 'Emailを入力して下さい。'}
206
+ validates :password, presence: {message: 'パスワードを入力して下さい。'}
207
+ end
208
+
209
+ ```