質問編集履歴
2
Sessions_helperを記載しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -163,3 +163,77 @@
|
|
163
163
|
|
164
164
|
|
165
165
|
```
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
###sessions_helper.rb
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
module SessionsHelper
|
176
|
+
|
177
|
+
def log_in(user)
|
178
|
+
|
179
|
+
session[:user_id]=user.id
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
def current_user
|
184
|
+
|
185
|
+
@current_user = User.find_by(id: session[:user_id])
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
def current_user?(user)
|
192
|
+
|
193
|
+
if session[:user_id]
|
194
|
+
|
195
|
+
user == current_user
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
def logged_in?
|
204
|
+
|
205
|
+
!current_user.nil?
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
def log_out
|
212
|
+
|
213
|
+
session.delete(:user_id)
|
214
|
+
|
215
|
+
@current_user=nil
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
private
|
222
|
+
|
223
|
+
def logged_in_user
|
224
|
+
|
225
|
+
unless logged_in?
|
226
|
+
|
227
|
+
redirect_to login_url
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
```
|
1
postsとapplicationsのコントローラー冒頭をアップしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -127,3 +127,39 @@
|
|
127
127
|
|
128
128
|
|
129
129
|
```
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
### 該当のソースコード
|
134
|
+
|
135
|
+
application_controller.rb の冒頭
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
class ApplicationController < ActionController::Base
|
140
|
+
|
141
|
+
include SessionsHelper
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
### 該当のソースコード
|
154
|
+
|
155
|
+
posts_controller.rb の冒頭
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
class PostsController < ApplicationController
|
160
|
+
|
161
|
+
before_action :logged_in_user, only:[:edit, :update, :destroy]
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
```
|