回答編集履歴
1
コードが間違っていたので修正・備忘録として動きなども自分なりに書き出してみました。
test
CHANGED
@@ -1,13 +1,22 @@
|
|
1
|
+
```ruby(users/show.html.erb)
|
1
|
-
|
2
|
+
<main>
|
3
|
+
<h1>User info</h1>
|
4
|
+
<%= image_tag @user.get_profile_image(100,100) %><br>
|
5
|
+
<h3>name <%= @user.name %></h3>
|
2
|
-
<h3>introduction <%= @user.user_introduction %></h3>
|
6
|
+
<h3>introduction <%= @user.user_introduction %></h3>
|
3
|
-
<% if @user == current_user.id %>
|
4
|
-
|
7
|
+
<p><%= link_to "プロフィール編集", edit_user_path(@user) %></p>
|
5
|
-
<% else %>
|
6
|
-
<% user_path(current_user.id) %>
|
7
|
-
<% end %>
|
8
|
-
<p><%= link_to "プロフィール編集", edit_user_path(@user) %></p>
|
9
8
|
```
|
10
|
-
これでユーザー一覧から投稿者の詳細ページに飛び、編集をクリックすると投稿者以外は自分の詳細ページに飛び、自分のユーザーに関する情報を編集できます。
|
11
|
-
|
9
|
+
```ruby(users_controller.rb)
|
12
|
-
|
10
|
+
before_action :correct_user, only: [:edit, :update]
|
13
11
|
|
12
|
+
def correct_user
|
13
|
+
user = User.find(params[:id])
|
14
|
+
if current_user.id != user.id
|
15
|
+
redirect_to user_path(current_user.id)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
```
|
19
|
+
自己解決したのですが、前回のものは間違えていたので改めて正しい記述を載せています。
|
20
|
+
before_actionでEditとUpdateのアクションに限って、実行する前にifと!=で投稿者のユーザーidと現在のログインユーザーのidが違っているか比較し、違っていたら現在ログインしているユーザーの詳細へ飛び、編集可能にする。
|
21
|
+
といった流れのようです(備忘録として動きを自分なりに書いてます。もしかしたら間違っているかも)
|
22
|
+
|