質問編集履歴
1
現状のエラーについて追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -69,3 +69,111 @@
|
|
69
69
|
end
|
70
70
|
|
71
71
|
```
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
##追記:修正後の現状で躓いている点
|
76
|
+
|
77
|
+
ApplicationController↓
|
78
|
+
|
79
|
+
```
|
80
|
+
|
81
|
+
class ApplicationController < ActionController::Base
|
82
|
+
|
83
|
+
before_action :set_current_user
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
def set_current_user
|
88
|
+
|
89
|
+
@current_user = User.find_by(id: session[:user_id])
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
topicコントローラー↓
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
def destroy
|
104
|
+
|
105
|
+
@topic = Topic.find_by(id: params[:id])
|
106
|
+
|
107
|
+
@topic.destroy
|
108
|
+
|
109
|
+
flash[:notice] = "トピックを削除しました"
|
110
|
+
|
111
|
+
if @current_user.admin
|
112
|
+
|
113
|
+
redirect_to(admin_path)
|
114
|
+
|
115
|
+
else
|
116
|
+
|
117
|
+
redirect_to("/")
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
userモデル↓
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
class User < ApplicationRecord
|
130
|
+
|
131
|
+
has_secure_password
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
validates :name, {presence: true}
|
136
|
+
|
137
|
+
validates :email, {presence: true, uniqueness: true}
|
138
|
+
|
139
|
+
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i
|
140
|
+
|
141
|
+
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX ,message: "形式が間違っています" }
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
has_many :topics
|
146
|
+
|
147
|
+
has_many :comments
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def admin
|
152
|
+
|
153
|
+
@current_user.admin_status == true
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
としています。
|
164
|
+
|
165
|
+
呼び出し方としてUserクラスにadminという
|
166
|
+
|
167
|
+
インスタンスメソッドを定義しています。
|
168
|
+
|
169
|
+
ApplicationControllerのbefore_actionで定義されている、@current_userを使い定義し実行しました。しかし今度は
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
@current_user.admin_status == true
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
でNoMethodErrorが発生してしまいます。。。
|
178
|
+
|
179
|
+
何がいけないのでしょうか?;;
|