質問編集履歴
5
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -188,119 +188,5 @@
|
|
188
188
|
|
189
189
|
### 補足情報(FW/ツールのバージョンなど)
|
190
190
|
|
191
|
-
|
191
|
+
ruby 2.7.1
|
192
|
-
Rails
|
192
|
+
Rails 6.0.3.2
|
193
|
-
|
194
|
-
### 実現したいこと
|
195
|
-
|
196
|
-
・自分以外のユーザーのプロフィールを編集できないようにする
|
197
|
-
・ユーザーのプロフィールページに置かれたリンクをクリックすることで、編集ページに移動
|
198
|
-
|
199
|
-
### 発生している問題・エラーメッセージ
|
200
|
-
|
201
|
-
上記の機能をprotectedメソッドとして定義してみたのですが、プロフィールの編集のリンクを押したところ、編集ページ(registrations#edit)に入れませんでした。
|
202
|
-
|
203
|
-
ブラウザには下記のエラーが表示されています。
|
204
|
-
|
205
|
-
```
|
206
|
-
ActiveRecord::RecordNotFound in Users::RegistrationsController#edit
|
207
|
-
|
208
|
-
Couldn't find User without an ID
|
209
|
-
|
210
|
-
# edit, updateアクションを現在のユーザーのみに限定する
|
211
|
-
def correct_user
|
212
|
-
@user = User.find(params[:id])
|
213
|
-
redirect_to(root_url) unless @user == current_user
|
214
|
-
end
|
215
|
-
```
|
216
|
-
|
217
|
-
|
218
|
-
### 該当のソースコード
|
219
|
-
Railstutorialを参考にprotectedメソッドにcorrect_userを定義しました。
|
220
|
-
ただ、これが原因でエラーが出ているようです。
|
221
|
-
|
222
|
-
```Ruby
|
223
|
-
# frozen_string_literal: true
|
224
|
-
|
225
|
-
class Users::RegistrationsController < Devise::RegistrationsController
|
226
|
-
before_action :authenticate_user!, only: [:edit, :update, :destroy]
|
227
|
-
before_action :correct_user, only: [:edit, :update]
|
228
|
-
|
229
|
-
def new
|
230
|
-
super
|
231
|
-
end
|
232
|
-
|
233
|
-
def create
|
234
|
-
super
|
235
|
-
end
|
236
|
-
|
237
|
-
def edit
|
238
|
-
super
|
239
|
-
end
|
240
|
-
|
241
|
-
def update
|
242
|
-
super
|
243
|
-
end
|
244
|
-
|
245
|
-
def destroy
|
246
|
-
super
|
247
|
-
end
|
248
|
-
|
249
|
-
protected
|
250
|
-
|
251
|
-
# edit, updateアクションを現在のユーザーのみに限定する
|
252
|
-
def correct_user
|
253
|
-
@user = User.find(params[:id])
|
254
|
-
redirect_to root_path unless @user == current_user
|
255
|
-
end
|
256
|
-
|
257
|
-
end
|
258
|
-
|
259
|
-
|
260
|
-
```
|
261
|
-
|
262
|
-
### 該当のview
|
263
|
-
こちらはdeviseと関係なく作成したユーザーのプロフィールページ(users#show)です。
|
264
|
-
|
265
|
-
```Ruby
|
266
|
-
<% provide(:title, @user.name) %>
|
267
|
-
|
268
|
-
<h3>プロフィール</h3>
|
269
|
-
<p>名前 : <%= @user.name %></p>
|
270
|
-
<p>メールアドレス: <%= @user.email %></p>
|
271
|
-
|
272
|
-
<%= link_to 'プロフィールの編集', edit_user_registration_path(@user) %>
|
273
|
-
```
|
274
|
-
|
275
|
-
### routes.rb
|
276
|
-
|
277
|
-
```Ruby
|
278
|
-
Rails.application.routes.draw do
|
279
|
-
root 'static_pages#home'
|
280
|
-
get '/help', to:'static_pages#help'
|
281
|
-
get '/about', to:'static_pages#about'
|
282
|
-
get '/contact', to:'static_pages#contact'
|
283
|
-
devise_for :users, :controllers => {
|
284
|
-
:confirmations => 'users/confirmations',
|
285
|
-
:registrations => 'users/registrations',
|
286
|
-
:sessions => 'users/sessions'
|
287
|
-
}
|
288
|
-
devise_scope :user do
|
289
|
-
get "sign_in", to:"users/sessions#new"
|
290
|
-
delete "sign_out", to:"users/sessions#destroy"
|
291
|
-
end
|
292
|
-
resources :users, only: [:index, :show]
|
293
|
-
end
|
294
|
-
|
295
|
-
```
|
296
|
-
|
297
|
-
### 試したこと、確認したこと
|
298
|
-
|
299
|
-
correct_userがなければ編集ページに移動できることは確認しており、その際URLにはユーザーのIDが記載されていました。なので、IDが見つからないからユーザーを検索できないというエラーの意味が
|
300
|
-
|
301
|
-
質問に不備がある場合は遠慮なく伝えていただけると幸いです。どうぞよろしくお願いします。
|
302
|
-
|
303
|
-
### 補足情報(FW/ツールのバージョンなど)
|
304
|
-
|
305
|
-
Ruby : ruby 2.7.1p83
|
306
|
-
Rails : Rails 6.0.3.2
|
4
記載漏れ
title
CHANGED
File without changes
|
body
CHANGED
@@ -104,8 +104,81 @@
|
|
104
104
|
|
105
105
|
```
|
106
106
|
|
107
|
-
### 試したこと
|
107
|
+
### 試したこと
|
108
|
+
Rspecでregistrations_controllerのテストをしました。
|
109
|
+
そのテストはパスするのですが、そもそも正しいテストを書けているか自信がありません。
|
108
110
|
|
111
|
+
念のため、以下にテスト内容とテストデータ(FactoryBot)を記載します。
|
112
|
+
```Ruby
|
113
|
+
require 'rails_helper'
|
114
|
+
|
115
|
+
describe Users::RegistrationsController, type: :controller do
|
116
|
+
let(:user) { create(:user) }
|
117
|
+
let(:other_user) { create(:other_user) }
|
118
|
+
|
119
|
+
# ユーザー情報編集ページの表示をテスト
|
120
|
+
describe 'GET #edit' do
|
121
|
+
# ログインしている場合
|
122
|
+
context 'with logged in' do
|
123
|
+
# edit(ユーザー情報編集)ページの検証
|
124
|
+
it 'responds successfully' do
|
125
|
+
@request.env["devise.mapping"] = Devise.mappings[:user]
|
126
|
+
sign_in user
|
127
|
+
get :edit, params: { id: user.id }
|
128
|
+
expect(response).to have_http_status "200"
|
129
|
+
expect(response).to render_template :edit
|
130
|
+
end
|
131
|
+
end
|
132
|
+
# ログインしていない場合
|
133
|
+
context 'without logged in' do
|
134
|
+
# edit(ユーザー情報編集)ページの代わりにsign_in(ログイン)ページを返すか検証
|
135
|
+
it 'redirect to new_session_path' do
|
136
|
+
@request.env["devise.mapping"] = Devise.mappings[:user]
|
137
|
+
get :edit, params: { id: user.id }
|
138
|
+
expect(response).to have_http_status "302"
|
139
|
+
expect(response).to redirect_to new_user_session_path
|
140
|
+
end
|
141
|
+
end
|
142
|
+
# 他のユーザーでログインしている場合
|
143
|
+
context 'with logged in as other user' do
|
144
|
+
# ルートにリダイレクトされるか検証
|
145
|
+
it 'redirect to root_path' do
|
146
|
+
@request.env["devise.mapping"] = Devise.mappings[:user]
|
147
|
+
sign_in other_user
|
148
|
+
get :edit, params: { id: user.id }
|
149
|
+
expect(response).to have_http_status "302"
|
150
|
+
expect(response).to redirect_to root_path
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
```Ruby
|
159
|
+
FactoryBot.define do
|
160
|
+
|
161
|
+
factory :user, class: User do
|
162
|
+
name { 'foo' }
|
163
|
+
email { 'foo@test.com' }
|
164
|
+
password { 'foo123' }
|
165
|
+
password_confirmation { 'foo123' }
|
166
|
+
confirmed_at { Date.today }
|
167
|
+
end
|
168
|
+
|
169
|
+
factory :other_user, class: User do
|
170
|
+
name { 'bar' }
|
171
|
+
email { 'bar@test.com' }
|
172
|
+
password { 'bar123' }
|
173
|
+
password_confirmation { 'bar123' }
|
174
|
+
confirmed_at { Date.today }
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
### 確認したこと
|
181
|
+
|
109
182
|
correct_userがなければ編集ページに移動できることは確認しており、その際URLにはユーザーのIDが記載されていました。そのため、IDが見つからないからユーザーを検索できないというエラーの原因がわかりませんでした。
|
110
183
|
|
111
184
|
1日悩んでわからなかったので、どなたか原因を教えていただけないでしょうか。
|
@@ -116,4 +189,118 @@
|
|
116
189
|
### 補足情報(FW/ツールのバージョンなど)
|
117
190
|
|
118
191
|
Ruby : ruby 2.7.1p83
|
192
|
+
Rails : Rails 6.0.3.2
|
193
|
+
|
194
|
+
### 実現したいこと
|
195
|
+
|
196
|
+
・自分以外のユーザーのプロフィールを編集できないようにする
|
197
|
+
・ユーザーのプロフィールページに置かれたリンクをクリックすることで、編集ページに移動
|
198
|
+
|
199
|
+
### 発生している問題・エラーメッセージ
|
200
|
+
|
201
|
+
上記の機能をprotectedメソッドとして定義してみたのですが、プロフィールの編集のリンクを押したところ、編集ページ(registrations#edit)に入れませんでした。
|
202
|
+
|
203
|
+
ブラウザには下記のエラーが表示されています。
|
204
|
+
|
205
|
+
```
|
206
|
+
ActiveRecord::RecordNotFound in Users::RegistrationsController#edit
|
207
|
+
|
208
|
+
Couldn't find User without an ID
|
209
|
+
|
210
|
+
# edit, updateアクションを現在のユーザーのみに限定する
|
211
|
+
def correct_user
|
212
|
+
@user = User.find(params[:id])
|
213
|
+
redirect_to(root_url) unless @user == current_user
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
|
218
|
+
### 該当のソースコード
|
219
|
+
Railstutorialを参考にprotectedメソッドにcorrect_userを定義しました。
|
220
|
+
ただ、これが原因でエラーが出ているようです。
|
221
|
+
|
222
|
+
```Ruby
|
223
|
+
# frozen_string_literal: true
|
224
|
+
|
225
|
+
class Users::RegistrationsController < Devise::RegistrationsController
|
226
|
+
before_action :authenticate_user!, only: [:edit, :update, :destroy]
|
227
|
+
before_action :correct_user, only: [:edit, :update]
|
228
|
+
|
229
|
+
def new
|
230
|
+
super
|
231
|
+
end
|
232
|
+
|
233
|
+
def create
|
234
|
+
super
|
235
|
+
end
|
236
|
+
|
237
|
+
def edit
|
238
|
+
super
|
239
|
+
end
|
240
|
+
|
241
|
+
def update
|
242
|
+
super
|
243
|
+
end
|
244
|
+
|
245
|
+
def destroy
|
246
|
+
super
|
247
|
+
end
|
248
|
+
|
249
|
+
protected
|
250
|
+
|
251
|
+
# edit, updateアクションを現在のユーザーのみに限定する
|
252
|
+
def correct_user
|
253
|
+
@user = User.find(params[:id])
|
254
|
+
redirect_to root_path unless @user == current_user
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
```
|
261
|
+
|
262
|
+
### 該当のview
|
263
|
+
こちらはdeviseと関係なく作成したユーザーのプロフィールページ(users#show)です。
|
264
|
+
|
265
|
+
```Ruby
|
266
|
+
<% provide(:title, @user.name) %>
|
267
|
+
|
268
|
+
<h3>プロフィール</h3>
|
269
|
+
<p>名前 : <%= @user.name %></p>
|
270
|
+
<p>メールアドレス: <%= @user.email %></p>
|
271
|
+
|
272
|
+
<%= link_to 'プロフィールの編集', edit_user_registration_path(@user) %>
|
273
|
+
```
|
274
|
+
|
275
|
+
### routes.rb
|
276
|
+
|
277
|
+
```Ruby
|
278
|
+
Rails.application.routes.draw do
|
279
|
+
root 'static_pages#home'
|
280
|
+
get '/help', to:'static_pages#help'
|
281
|
+
get '/about', to:'static_pages#about'
|
282
|
+
get '/contact', to:'static_pages#contact'
|
283
|
+
devise_for :users, :controllers => {
|
284
|
+
:confirmations => 'users/confirmations',
|
285
|
+
:registrations => 'users/registrations',
|
286
|
+
:sessions => 'users/sessions'
|
287
|
+
}
|
288
|
+
devise_scope :user do
|
289
|
+
get "sign_in", to:"users/sessions#new"
|
290
|
+
delete "sign_out", to:"users/sessions#destroy"
|
291
|
+
end
|
292
|
+
resources :users, only: [:index, :show]
|
293
|
+
end
|
294
|
+
|
295
|
+
```
|
296
|
+
|
297
|
+
### 試したこと、確認したこと
|
298
|
+
|
299
|
+
correct_userがなければ編集ページに移動できることは確認しており、その際URLにはユーザーのIDが記載されていました。なので、IDが見つからないからユーザーを検索できないというエラーの意味が
|
300
|
+
|
301
|
+
質問に不備がある場合は遠慮なく伝えていただけると幸いです。どうぞよろしくお願いします。
|
302
|
+
|
303
|
+
### 補足情報(FW/ツールのバージョンなど)
|
304
|
+
|
305
|
+
Ruby : ruby 2.7.1p83
|
119
306
|
Rails : Rails 6.0.3.2
|
3
説明不足
title
CHANGED
File without changes
|
body
CHANGED
@@ -69,6 +69,8 @@
|
|
69
69
|
|
70
70
|
### 該当のview
|
71
71
|
こちらはdeviseと関係なく作成したユーザーのプロフィールページ(users#show)です。
|
72
|
+
「プロフィールの編集」リンクを押して、プロフィール編集画面に移動する際に上記のエラーがでました。
|
73
|
+
このページは正しく表示されています。
|
72
74
|
|
73
75
|
```Ruby
|
74
76
|
<% provide(:title, @user.name) %>
|
2
説明不足
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
|
3
|
+
・deviseでユーザー認証機能を導入したい
|
3
|
-
・自分以外のユーザーのプロフィールを編集できないように
|
4
|
+
・その過程で、自分以外のユーザーのプロフィールを編集できないようにしたい
|
4
|
-
・ユーザーのプロフィールページに置かれたリンクをクリックすることで、編集ページに移動
|
5
|
+
・ユーザーのプロフィールページに置かれたリンクをクリックすることで、プロフィールの編集ページに移動させたい
|
5
6
|
|
6
7
|
### 発生している問題・エラーメッセージ
|
7
8
|
|
1
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -103,10 +103,13 @@
|
|
103
103
|
|
104
104
|
### 試したこと、確認したこと
|
105
105
|
|
106
|
-
correct_userがなければ編集ページに移動できることは確認しており、その際URLにはユーザーのIDが記載されていました。
|
106
|
+
correct_userがなければ編集ページに移動できることは確認しており、その際URLにはユーザーのIDが記載されていました。そのため、IDが見つからないからユーザーを検索できないというエラーの原因がわかりませんでした。
|
107
107
|
|
108
|
+
1日悩んでわからなかったので、どなたか原因を教えていただけないでしょうか。
|
108
|
-
質問に不備がある場合は遠慮なく伝えていただけると幸いです。
|
109
|
+
質問に不備がある場合は遠慮なく伝えていただけると幸いです。
|
109
110
|
|
111
|
+
どうぞよろしくお願いします。
|
112
|
+
|
110
113
|
### 補足情報(FW/ツールのバージョンなど)
|
111
114
|
|
112
115
|
Ruby : ruby 2.7.1p83
|