質問編集履歴
8
具体例反映
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
RailsのDeviseを使って、アカウント編集の実装を行っています。
|
2
2
|
以下の内容を参考に実装しているのですが、現在のパスワードの入力は不要になりましたが、毎回パスワード変更を行わないと正しく更新されないようになってしまいます。
|
3
|
+
https://kossy-web-engineer.hatenablog.com/entry/2018/11/06/102047
|
3
4
|
|
4
5
|

|
5
6
|
上記のようにパスワードの変更を行わないとエラーが起きてしまいます。
|
@@ -58,11 +59,59 @@
|
|
58
59
|
|
59
60
|
|
60
61
|
#user.rb
|
62
|
+
|
63
|
+
class User < ApplicationRecord
|
64
|
+
# Include default devise modules. Others available are:
|
65
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
61
|
-
devise :database_authenticatable, :registerable,
|
66
|
+
devise :database_authenticatable, :registerable,
|
62
67
|
:recoverable, :rememberable, :validatable
|
63
68
|
|
69
|
+
validates :name,:useruserid, presence: true #追記
|
70
|
+
# @([a-zA-Z0-9_-])
|
71
|
+
mount_uploader :image, ImageUploader
|
72
|
+
validates :useruserid, presence: true, uniqueness: true, format: { with: /\A@[\w+\-.]+\z/i }
|
73
|
+
validates :email, presence: true, uniqueness: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }
|
74
|
+
validates :password, presence: true, format: { with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,40}+\z/i }
|
75
|
+
# validates :profile, length: { maximum: 200 } #追記
|
76
|
+
has_many :comments
|
77
|
+
has_many :tweets, dependent: :destroy
|
78
|
+
has_many :likes, dependent: :destroy
|
79
|
+
has_many :liked_tweets, through: :likes, source: :tweet
|
64
80
|
|
81
|
+
has_many :following_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy
|
82
|
+
|
83
|
+
has_many :followings, through: :following_relationships
|
84
|
+
|
85
|
+
has_many :follower_relationships, foreign_key: "following_id", class_name: "Relationship", dependent: :destroy
|
86
|
+
|
87
|
+
has_many :followers, through: :follower_relationships
|
88
|
+
|
89
|
+
# has_many :temps
|
90
|
+
|
91
|
+
def following?(other_user)
|
92
|
+
following_relationships.find_by(following_id: other_user.id)
|
93
|
+
end
|
94
|
+
|
95
|
+
def follow!(other_user)
|
96
|
+
following_relationships.create!(following_id: other_user.id)
|
97
|
+
end
|
98
|
+
|
99
|
+
def unfollow!(other_user)
|
100
|
+
following_relationships.find_by(following_id: other_user.id).destroy
|
101
|
+
end
|
102
|
+
|
103
|
+
def already_liked?(tweet)
|
104
|
+
self.likes.exists?(tweet_id: tweet.id)
|
105
|
+
end
|
106
|
+
def self.search(search)
|
107
|
+
if search
|
108
|
+
User.where('name LIKE(?) or sex LIKE(?) ', "%#{search}%","%#{search}%")
|
109
|
+
else
|
110
|
+
User.all
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
65
|
-
def update_without_current_password(params, *options)
|
114
|
+
def update_without_current_password(params, *options)
|
66
115
|
params.delete(:current_password)
|
67
116
|
|
68
117
|
if params[:password].blank? && params[:password_confirmation].blank?
|
@@ -70,12 +119,17 @@
|
|
70
119
|
params.delete(:password_confirmation)
|
71
120
|
end
|
72
121
|
|
122
|
+
binding.pry
|
73
123
|
result = update_attributes(params, *options)
|
74
124
|
clean_up_passwords
|
75
125
|
result
|
76
126
|
end
|
77
127
|
|
128
|
+
end
|
78
129
|
|
130
|
+
|
131
|
+
|
132
|
+
|
79
133
|
#routes.rb
|
80
134
|
|
81
135
|
devise_for :users,
|
@@ -195,5 +249,5 @@
|
|
195
249
|
result = update_attributes(params, *options)
|
196
250
|
上記のコードでエラーが起きました。
|
197
251
|
```
|
198
|
-
|
252
|
+
binding.pryを実行した結果
|
199
|
-

|
7
画像添付
title
CHANGED
File without changes
|
body
CHANGED
@@ -194,4 +194,6 @@
|
|
194
194
|
|
195
195
|
result = update_attributes(params, *options)
|
196
196
|
上記のコードでエラーが起きました。
|
197
|
-
```
|
197
|
+
```
|
198
|
+
rails cを実行した結果
|
199
|
+

|
6
ファイル追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -178,4 +178,20 @@
|
|
178
178
|
|
179
179
|
<%= link_to "Back", :back %>
|
180
180
|
|
181
|
+
```
|
182
|
+
|
183
|
+
```
|
184
|
+
#編集後(useruserid追加)
|
185
|
+
エラー画面
|
186
|
+
|
187
|
+
↳ app/models/user.rb:60
|
188
|
+
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`useruserid` = BINARY '@aaaa' AND `users`.`id` != 1 LIMIT 1
|
189
|
+
↳ app/models/user.rb:60
|
190
|
+
User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaa@gmail.com' AND `users`.`id` != 1 LIMIT 1
|
191
|
+
↳ app/models/user.rb:60
|
192
|
+
(0.2ms) ROLLBACK
|
193
|
+
↳ app/models/user.rb:60
|
194
|
+
|
195
|
+
result = update_attributes(params, *options)
|
196
|
+
上記のコードでエラーが起きました。
|
181
197
|
```
|
5
ファイル追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -130,4 +130,52 @@
|
|
130
130
|
↳ app/models/user.rb:59
|
131
131
|
(3.6ms) ROLLBACK
|
132
132
|
↳ app/models/user.rb:59
|
133
|
+
```
|
134
|
+
```
|
135
|
+
#元々あったregistrations/edit.html.haml
|
136
|
+
|
137
|
+
<h2>Edit <%= resource_name.to_s.humanize %></h2>
|
138
|
+
|
139
|
+
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
|
140
|
+
<%= render "devise/shared/error_messages", resource: resource %>
|
141
|
+
|
142
|
+
<div class="field">
|
143
|
+
<%= f.label :email %><br />
|
144
|
+
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
|
145
|
+
</div>
|
146
|
+
|
147
|
+
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
|
148
|
+
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
|
149
|
+
<% end %>
|
150
|
+
|
151
|
+
<div class="field">
|
152
|
+
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
|
153
|
+
<%= f.password_field :password, autocomplete: "new-password" %>
|
154
|
+
<% if @minimum_password_length %>
|
155
|
+
<br />
|
156
|
+
<em><%= @minimum_password_length %> characters minimum</em>
|
157
|
+
<% end %>
|
158
|
+
</div>
|
159
|
+
|
160
|
+
<div class="field">
|
161
|
+
<%= f.label :password_confirmation %><br />
|
162
|
+
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
|
163
|
+
</div>
|
164
|
+
|
165
|
+
<div class="field">
|
166
|
+
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
|
167
|
+
<%= f.password_field :current_password, autocomplete: "current-password" %>
|
168
|
+
</div>
|
169
|
+
|
170
|
+
<div class="actions">
|
171
|
+
<%= f.submit "Update" %>
|
172
|
+
</div>
|
173
|
+
<% end %>
|
174
|
+
|
175
|
+
<h3>Cancel my account</h3>
|
176
|
+
|
177
|
+
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
|
178
|
+
|
179
|
+
<%= link_to "Back", :back %>
|
180
|
+
|
133
181
|
```
|
4
文記載
title
CHANGED
File without changes
|
body
CHANGED
@@ -120,6 +120,7 @@
|
|
120
120
|
result
|
121
121
|
|
122
122
|
#上記の部分でエラーが起きていました。
|
123
|
+
エラー文は以下のものになります。
|
123
124
|
Unpermitted parameter: :useruserid
|
124
125
|
(1.0ms) BEGIN
|
125
126
|
↳ app/models/user.rb:59
|
3
エラー情報更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -112,4 +112,21 @@
|
|
112
112
|
end
|
113
113
|
|
114
114
|
```
|
115
|
-
現在のパスワードは空でも更新できますが、パスワード変更なしの記事が見当たらなかったので質問させていただきました。原因特定できる記事もなかったためアドバイスまたは提案をお願いします
|
115
|
+
現在のパスワードは空でも更新できますが、パスワード変更なしの記事が見当たらなかったので質問させていただきました。原因特定できる記事もなかったためアドバイスまたは提案をお願いします
|
116
|
+
|
117
|
+
```
|
118
|
+
result = update_attributes(params, *options)
|
119
|
+
clean_up_passwords
|
120
|
+
result
|
121
|
+
|
122
|
+
#上記の部分でエラーが起きていました。
|
123
|
+
Unpermitted parameter: :useruserid
|
124
|
+
(1.0ms) BEGIN
|
125
|
+
↳ app/models/user.rb:59
|
126
|
+
User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`useruserid` = BINARY '@aaa' AND `users`.`id` != 1 LIMIT 1
|
127
|
+
↳ app/models/user.rb:59
|
128
|
+
User Exists (11.9ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaa@gmail.com' AND `users`.`id` != 1 LIMIT 1
|
129
|
+
↳ app/models/user.rb:59
|
130
|
+
(3.6ms) ROLLBACK
|
131
|
+
↳ app/models/user.rb:59
|
132
|
+
```
|
2
画像追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
RailsのDeviseを使って、アカウント編集の実装を行っています。
|
2
2
|
以下の内容を参考に実装しているのですが、現在のパスワードの入力は不要になりましたが、毎回パスワード変更を行わないと正しく更新されないようになってしまいます。
|
3
|
+
|
4
|
+

|
5
|
+
上記のようにパスワードの変更を行わないとエラーが起きてしまいます。
|
3
6
|
https://kossy-web-engineer.hatenablog.com/entry/2018/11/06/102047
|
4
7
|
|
5
8
|
```ここに言語を入力
|
1
コントローラーの詳細記載
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【Rails】Devise アカウント編集 パスワードなし
|
1
|
+
【Rails】Devise アカウント編集 パスワードなしで更新
|
body
CHANGED
@@ -88,5 +88,25 @@
|
|
88
88
|
resource.update_without_current_password(params)
|
89
89
|
end
|
90
90
|
end
|
91
|
+
|
92
|
+
#ApplicationController.rb
|
93
|
+
class ApplicationController < ActionController::Base
|
94
|
+
protect_from_forgery with: :exception
|
95
|
+
before_action :authenticate_user!
|
96
|
+
before_action :configure_permitted_parameters, if: :devise_controller?
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
def configure_permitted_parameters
|
101
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:useruserid])
|
102
|
+
devise_parameter_sanitizer.permit(:account_update, keys: [:name, :profile,:image,:sex,:age,:tall])
|
103
|
+
end
|
104
|
+
|
105
|
+
def log_in(user)
|
106
|
+
session[:user_id] = user.id
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
91
111
|
```
|
92
|
-
現在のパスワードは空でも更新できますが、パスワード変更なしの記事が見当たらなかったので質問させていただきました。
|
112
|
+
現在のパスワードは空でも更新できますが、パスワード変更なしの記事が見当たらなかったので質問させていただきました。原因特定できる記事もなかったためアドバイスまたは提案をお願いします
|