質問編集履歴

5

change

2017/08/10 12:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,411 +1,37 @@
1
- 新規登録をても、ユーザーページではなく、なぜログインフォーム画面に変わります
1
+ どこが原因なのでょう
2
2
 
3
- エラーメッセージも表示れず、変わるので、わからな状態です
3
+ 教えてください!
4
4
 
5
+ どこが原因なのでしょうか?
5
6
 
6
-
7
- ###users_controller.rb
8
-
9
- ```Ruby
10
-
11
- class UsersController < ApplicationController
12
-
13
- before_action :authenticate_user, {only: [:index, :show, :edit, :update]}
14
-
15
- before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
16
-
17
- before_action :ensure_correct_user, {only: [:edit, :update]}
18
-
19
-
20
-
21
- def index
22
-
23
- @users = User.all
24
-
25
- end
26
-
27
-
28
-
29
- def show
7
+ 教えてください!
30
-
31
- @user = User.find_by(id: params[:id])
32
-
33
- end
34
-
35
-
36
-
37
- def new
38
-
39
- @user = User.new
40
-
41
- end
42
-
43
-
44
-
45
- def create
46
-
47
- @user = User.new(
48
-
49
- name: params[:name],
50
-
51
- email: params[:email],
52
-
53
- image_name: "default_user.jpg",
54
-
55
- password: params[:password]
56
-
57
- )
58
-
59
- if @user.save
60
-
61
- session[:user_id] = @user.id
62
-
63
- flash[:notice] = "ユーザー登録が完了しました"
64
-
65
- redirect_to("/users/#{@user.id}")
66
-
67
- else
68
-
69
- render("users/new")
70
-
71
- end
72
-
73
- end
74
-
75
-
76
-
77
- def edit
78
-
79
- @user = User.find_by(id: params[:id])
80
-
81
- end
82
-
83
-
84
-
85
- def update
86
-
87
- @user = User.find_by(id: params[:id])
88
-
89
- @user.name = params[:name]
90
-
91
- @user.email = params[:email]
92
-
93
-
94
-
95
- if params[:image]
96
-
97
- @user.image_name = "#{@user.id}.jpg"
98
-
99
- image = params[:image]
100
-
101
- File.binwrite("public/user_images/#{@user.image_name}", image.read)
102
-
103
- end
104
-
105
-
106
-
107
- if @user.save
108
-
109
- flash[:notice] = "ユーザー情報を編集しました"
110
-
111
- redirect_to("/users/#{@user.id}")
112
-
113
- else
114
-
115
- render("users/edit")
116
-
117
- end
118
-
119
- end
120
-
121
-
122
-
123
- def login_form
124
-
125
- end
126
-
127
-
128
-
129
- def login
130
-
131
- @user = User.find_by(email: params[:email], password: params[:password])
132
-
133
- if @user
134
-
135
- session[:user_id] = @user.id
136
-
137
- flash[:notice] = "ログインしました"
138
-
139
- redirect_to("/posts/index")
140
-
141
- else
142
-
143
- @error_message = "メールアドレスまたはパスワードが間違っています"
144
-
145
- @email = params[:email]
146
-
147
- @password = params[:password]
148
-
149
- render("users/login_form")
150
-
151
- end
152
-
153
- end
154
-
155
-
156
-
157
- def logout
158
-
159
- session[:user_id] = nil
160
-
161
- flash[:notice] = "ログアウトしました"
162
-
163
- redirect_to("/login")
164
-
165
- end
166
-
167
-
168
-
169
- def likes
170
-
171
- # 変数@userを定義してください
172
-
173
- @user = User.find_by(id: params[:id])
174
-
175
-
176
-
177
- # 変数@likesを定義してください
178
-
179
- @likes = Like.where(user_id: @user.id)
180
-
181
-
182
-
183
- end
184
-
185
-
186
-
187
- def ensure_correct_user
188
-
189
- if @current_user.id != params[:id].to_i
190
-
191
- flash[:notice] = "権限がありません"
192
-
193
- redirect_to("/posts/index")
194
-
195
- end
196
-
197
- end
198
-
199
-
200
-
201
- end
202
-
203
-
204
-
205
- ```
206
-
207
- ###new.html.erb
208
-
209
- ```Ruby
210
-
211
- <div class="main users-new">
212
-
213
- <div class="container">
214
-
215
- <div class="form-heading">新規ユーザー登録</div>
216
-
217
- <div class="form users-form">
218
-
219
- <div class="form-body">
220
-
221
- <% @user.errors.full_messages.each do |message| %>
222
-
223
- <div class="form-error">
224
-
225
- <%= message %>
226
-
227
- </div>
228
-
229
- <% end %>
230
-
231
-
232
-
233
- <%= form_tag("/users/create") do %>
234
-
235
- <p>ユーザー名</p>
236
-
237
- <input name="name" value="<%= @user.name %>">
238
-
239
- <p>メールアドレス</p>
240
-
241
- <input name="email" value="<%= @user.email %>">
242
-
243
- <p>パスワード</p>
244
-
245
- <input type="password" name="password" value="<%= @user.password %>">
246
-
247
- <input type="submit" value="新規登録">
248
-
249
- <% end %>
250
-
251
- </div>
252
-
253
- </div>
254
-
255
- </div>
256
-
257
- </div>
258
-
259
- ```
260
-
261
- ###routes.rb
262
-
263
- ```Ruby
264
-
265
- post "users/:id/update" => "users#update"
266
-
267
- get "users/:id/edit" => "users#edit"
268
-
269
- post "users/create" => "users#create"
270
-
271
- get "signup" => "users#new"
272
-
273
- get "users/index" => "users#index"
274
-
275
- get "users/:id" => "users#show"
276
-
277
- post "login" => "users#login"
278
-
279
- post "logout" => "users#logout"
280
-
281
- get "login" => "users#login_form"
282
-
283
- get "users/:id/likes" => "users#likes"
284
-
285
-
286
-
287
- ```
288
-
289
- ###gem
290
-
291
- ```ここに言語を入力
292
-
293
- source 'https://rubygems.org'
294
-
295
-
296
-
297
- gem 'rails', '5.0.3'
298
-
299
- # Use Puma as the app server
300
-
301
- gem 'puma', '3.6.2'
302
-
303
- # Use SCSS for stylesheets
304
-
305
- gem 'sass-rails', '5.0.6'
306
-
307
- # Use Uglifier as compressor for JavaScript assets
308
-
309
- gem 'uglifier', '3.0.4'
310
-
311
- gem 'pry-rails', '0.3.4'
312
-
313
- # Use jquery as the JavaScript library
314
-
315
- gem 'jquery-rails', '4.2.2'
316
-
317
- # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
318
-
319
- gem 'turbolinks', '5.0.1'
320
-
321
- gem 'bcrypt', '3.1.11'
322
-
323
-
324
-
325
- group :development, :test do
326
-
327
- gem 'sqlite3', '1.3.13'
328
-
329
- gem 'byebug', '9.0.6', platform: :mri
330
-
331
-
332
-
333
- gem 'web-console', '3.4.0'
334
-
335
- gem 'listen', '3.0.8'
336
-
337
- # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
338
-
339
- gem 'spring', '2.0.1'
340
-
341
- gem 'spring-watcher-listen', '2.0.1'
342
-
343
- end
344
-
345
-
346
-
347
- ```
348
-
349
- ###ApplicationController
350
-
351
- ```ここに言語を入力
352
-
353
- class ApplicationController < ActionController::Base
354
-
355
- before_action :set_current_user
356
-
357
-
358
-
359
- def set_current_user
360
-
361
- @current_user = User.find_by(id: session[:user_id])
362
-
363
- end
364
-
365
-
366
-
367
- def authenticate_user
368
-
369
- if @current_user == nil
370
-
371
- flash[:notice] = "ログインが必要です"
372
-
373
- redirect_to("/login")
374
-
375
- end
376
-
377
- end
378
-
379
-
380
-
381
- def forbid_login_user
382
-
383
- if @current_user
384
-
385
- flash[:notice] = "すでにログインしています"
386
-
387
- redirect_to("/posts/index")
388
-
389
- end
390
-
391
- end
392
-
393
-
394
-
395
- end
396
-
397
-
398
-
399
- ```
400
-
401
-
402
-
403
-
404
-
405
-
406
8
 
407
9
 
408
10
 
409
11
  どこが原因なのでしょうか?
410
12
 
411
13
  教えてください!
14
+
15
+ どこが原因なのでしょうか?
16
+
17
+ 教えてください!
18
+
19
+ どこが原因なのでしょうか?
20
+
21
+ 教えてください!
22
+
23
+ どこが原因なのでしょうか?
24
+
25
+ 教えてください!
26
+
27
+ どこが原因なのでしょうか?
28
+
29
+ 教えてください!
30
+
31
+ どこが原因なのでしょうか?
32
+
33
+ 教えてください!
34
+
35
+ どこが原因なのでしょうか?
36
+
37
+ 教えてください!

4

ApplicationControllerの追記

2017/08/10 12:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -346,6 +346,62 @@
346
346
 
347
347
  ```
348
348
 
349
+ ###ApplicationController
350
+
351
+ ```ここに言語を入力
352
+
353
+ class ApplicationController < ActionController::Base
354
+
355
+ before_action :set_current_user
356
+
357
+
358
+
359
+ def set_current_user
360
+
361
+ @current_user = User.find_by(id: session[:user_id])
362
+
363
+ end
364
+
365
+
366
+
367
+ def authenticate_user
368
+
369
+ if @current_user == nil
370
+
371
+ flash[:notice] = "ログインが必要です"
372
+
373
+ redirect_to("/login")
374
+
375
+ end
376
+
377
+ end
378
+
379
+
380
+
381
+ def forbid_login_user
382
+
383
+ if @current_user
384
+
385
+ flash[:notice] = "すでにログインしています"
386
+
387
+ redirect_to("/posts/index")
388
+
389
+ end
390
+
391
+ end
392
+
393
+
394
+
395
+ end
396
+
397
+
398
+
399
+ ```
400
+
401
+
402
+
403
+
404
+
349
405
 
350
406
 
351
407
 

3

gemの追記

2017/06/27 03:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -286,6 +286,68 @@
286
286
 
287
287
  ```
288
288
 
289
+ ###gem
290
+
291
+ ```ここに言語を入力
292
+
293
+ source 'https://rubygems.org'
294
+
295
+
296
+
297
+ gem 'rails', '5.0.3'
298
+
299
+ # Use Puma as the app server
300
+
301
+ gem 'puma', '3.6.2'
302
+
303
+ # Use SCSS for stylesheets
304
+
305
+ gem 'sass-rails', '5.0.6'
306
+
307
+ # Use Uglifier as compressor for JavaScript assets
308
+
309
+ gem 'uglifier', '3.0.4'
310
+
311
+ gem 'pry-rails', '0.3.4'
312
+
313
+ # Use jquery as the JavaScript library
314
+
315
+ gem 'jquery-rails', '4.2.2'
316
+
317
+ # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
318
+
319
+ gem 'turbolinks', '5.0.1'
320
+
321
+ gem 'bcrypt', '3.1.11'
322
+
323
+
324
+
325
+ group :development, :test do
326
+
327
+ gem 'sqlite3', '1.3.13'
328
+
329
+ gem 'byebug', '9.0.6', platform: :mri
330
+
331
+
332
+
333
+ gem 'web-console', '3.4.0'
334
+
335
+ gem 'listen', '3.0.8'
336
+
337
+ # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
338
+
339
+ gem 'spring', '2.0.1'
340
+
341
+ gem 'spring-watcher-listen', '2.0.1'
342
+
343
+ end
344
+
345
+
346
+
347
+ ```
348
+
349
+
350
+
289
351
 
290
352
 
291
353
  どこが原因なのでしょうか?

2

classの表記

2017/06/27 03:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,16 @@
8
8
 
9
9
  ```Ruby
10
10
 
11
+ class UsersController < ApplicationController
12
+
13
+ before_action :authenticate_user, {only: [:index, :show, :edit, :update]}
14
+
15
+ before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]}
16
+
17
+ before_action :ensure_correct_user, {only: [:edit, :update]}
18
+
19
+
20
+
11
21
  def index
12
22
 
13
23
  @users = User.all
@@ -154,6 +164,44 @@
154
164
 
155
165
  end
156
166
 
167
+
168
+
169
+ def likes
170
+
171
+ # 変数@userを定義してください
172
+
173
+ @user = User.find_by(id: params[:id])
174
+
175
+
176
+
177
+ # 変数@likesを定義してください
178
+
179
+ @likes = Like.where(user_id: @user.id)
180
+
181
+
182
+
183
+ end
184
+
185
+
186
+
187
+ def ensure_correct_user
188
+
189
+ if @current_user.id != params[:id].to_i
190
+
191
+ flash[:notice] = "権限がありません"
192
+
193
+ redirect_to("/posts/index")
194
+
195
+ end
196
+
197
+ end
198
+
199
+
200
+
201
+ end
202
+
203
+
204
+
157
205
  ```
158
206
 
159
207
  ###new.html.erb

1

タグの追加

2017/06/27 03:07

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes