質問編集履歴

3

StaticPagesコントローラーを追加しました。

2020/05/14 12:58

投稿

mayok
mayok

スコア12

test CHANGED
File without changes
test CHANGED
@@ -414,6 +414,18 @@
414
414
 
415
415
  ```
416
416
 
417
+ ```
418
+
419
+ (static_pages_controller.rb)
420
+
421
+ class StaticPagesController < ApplicationController
422
+
423
+ def home; end
424
+
425
+ end
426
+
427
+ ```
428
+
417
429
 
418
430
 
419
431
  ### 試したこと

2

プロフィールのリンクを修正、コンソールログを追加しました。

2020/05/14 12:58

投稿

mayok
mayok

スコア12

test CHANGED
File without changes
test CHANGED
@@ -344,7 +344,7 @@
344
344
 
345
345
  <li class="signedin-item py-4">
346
346
 
347
- <%= link_to user_path(@user.id), class: "signedin-item text-decoration-none" do %>
347
+ <%= link_to user_path(current_user), class: "signedin-item text-decoration-none" do %>
348
348
 
349
349
  <i class="far fa-user-circle mr-2 nav-items"></i> プロフィール
350
350
 
@@ -432,6 +432,36 @@
432
432
 
433
433
  ・```発生しているエラーメッセージ```の項目でコードを載せた通り、パラメーターの値が"id" => "6"となるはずなのに"format" => "6"と出力されている。
434
434
 
435
+ **追記:その際の挙動は以下になります**
436
+
437
+
438
+
439
+ ```
440
+
441
+ Started GET "/user.6" for ::1 at 2020-05-14 21:33:41 +0900
442
+
443
+ (0.7ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
444
+
445
+ (0.5ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
446
+
447
+ Processing by UsersController#show as
448
+
449
+ Completed 500 in 20ms (ActiveRecord: 3.5ms | Allocations: 4928)
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+ ActiveRecord::RecordNotFound (Couldn't find User without an ID):
458
+
459
+
460
+
461
+ app/controllers/users_controller.rb:70:in `set_user'
462
+
463
+ ```
464
+
435
465
 
436
466
 
437
467
  ・ログインユーザーのプロフィールを表示するためリンクを押すと、URLが```/user.1```となってしまう。

1

controlller、viewを追加しました。

2020/05/14 12:46

投稿

mayok
mayok

スコア12

test CHANGED
File without changes
test CHANGED
@@ -92,9 +92,129 @@
92
92
 
93
93
  (users_controller.rb)
94
94
 
95
+ class UsersController < ApplicationController
96
+
97
+ before_action :set_user, only: [:show, :edit, :update, :destroy]
98
+
99
+
100
+
101
+ # GET /users
102
+
103
+ # GET /users.json
104
+
105
+ def index
106
+
107
+ @users = User.all
108
+
109
+ end
110
+
111
+
112
+
113
+ # GET /users/1
114
+
115
+ # GET /users/1.json
116
+
95
117
  def show
96
118
 
97
- @user = User.find(params[:id])
119
+ @user = User.find(params[:id])
120
+
121
+ end
122
+
123
+
124
+
125
+ # GET /users/new
126
+
127
+ # def new
128
+
129
+ # @user = User.new
130
+
131
+ # end
132
+
133
+
134
+
135
+ # GET /users/1/edit
136
+
137
+ def edit
138
+
139
+ @user = User.find(params[:id])
140
+
141
+ end
142
+
143
+
144
+
145
+ # POST /users
146
+
147
+ # POST /users.json
148
+
149
+ def create
150
+
151
+ @user = User.new(user_params)
152
+
153
+ respond_to do |format|
154
+
155
+ if @user.save
156
+
157
+ format.html { redirect_to @user, notice: 'User was successfully created.' }
158
+
159
+ format.json { render :show, status: :created, location: @user }
160
+
161
+ else
162
+
163
+ format.html { render :new }
164
+
165
+ format.json { render json: @user.errors, status: :unprocessable_entity }
166
+
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+
174
+
175
+ # PATCH/PUT /users/1
176
+
177
+ # PATCH/PUT /users/1.json
178
+
179
+ def update
180
+
181
+ respond_to do |format|
182
+
183
+ if @user.update(user_params)
184
+
185
+ format.html { redirect_to @user, notice: 'User was successfully updated.' }
186
+
187
+ format.json { render :show, status: :ok, location: @user }
188
+
189
+ else
190
+
191
+ format.html { render :edit }
192
+
193
+ format.json { render json: @user.errors, status: :unprocessable_entity }
194
+
195
+ end
196
+
197
+ end
198
+
199
+ end
200
+
201
+
202
+
203
+ # DELETE /users/1
204
+
205
+ # DELETE /users/1.json
206
+
207
+ def destroy
208
+
209
+ @user.destroy
210
+
211
+ respond_to do |format|
212
+
213
+ format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
214
+
215
+ format.json { head :no_content }
216
+
217
+ end
98
218
 
99
219
  end
100
220
 
@@ -104,19 +224,193 @@
104
224
 
105
225
 
106
226
 
227
+ # Use callbacks to share common setup or constraints between actions.
228
+
107
229
  def set_user
108
230
 
109
231
  @user = User.find(params[:id])
110
232
 
111
233
  end
112
234
 
113
- ```
114
-
115
-
116
-
117
- ```
118
-
119
- <%= link_to user_path(@user.id), class: "signedin-item text-decoration-none" do %>
235
+
236
+
237
+ # Never trust parameters from the scary internet, only allow the white list through.
238
+
239
+ def user_params
240
+
241
+ params.require(:user).permit(:name, :email)
242
+
243
+ end
244
+
245
+ end
246
+
247
+
248
+
249
+ ```
250
+
251
+
252
+
253
+ ```
254
+
255
+ (application.html.erb)
256
+
257
+ <!DOCTYPE html>
258
+
259
+ <html lang="ja">
260
+
261
+ <head>
262
+
263
+ <title><%= yield(:title) %> | Contrail</title>
264
+
265
+ <%= csrf_meta_tags %>
266
+
267
+ <%= csp_meta_tag %>
268
+
269
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
270
+
271
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
272
+
273
+ <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
274
+
275
+ <meta charset="utf-8">
276
+
277
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
278
+
279
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous">
280
+
281
+ </head>
282
+
283
+  <body>
284
+
285
+ <header>
286
+
287
+ <%= render "shared/header" %>
288
+
289
+ </header>
290
+
291
+ <main>
292
+
293
+ <%= render "shared/main" %>
294
+
295
+ <%= yield %>
296
+
297
+ </main>
298
+
299
+ <footer class="fixed-bottom text-center text-muted py-4">
300
+
301
+ <%= render "shared/footer" %>
302
+
303
+ </footer>
304
+
305
+
306
+
307
+ <%= render "shared/js" %>
308
+
309
+ </body>
310
+
311
+ </html>
312
+
313
+ ```
314
+
315
+ ```
316
+
317
+ (application.html.erb内 _header.html.erb)
318
+
319
+ <nav class="navbar sticky-top mb-4">
320
+
321
+ <%=link_to "Contrail", root_path, class: "navbar-brand text-monospace" %>
322
+
323
+ <li>
324
+
325
+ <%=link_to class: "fa-heart" do %>
326
+
327
+ <i class="far fa-heart fa-lg"></i>
328
+
329
+ <% end %>
330
+
331
+ </li>
332
+
333
+ <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="ナビゲーションの切替">
334
+
335
+ <i class="fas fa-bars fa-lg"></i>
336
+
337
+ </button>
338
+
339
+ <div class="collapse navbar-collapse text-left" id="navbarNavAltMarkup">
340
+
341
+ <% if user_signed_in? %>
342
+
343
+ <ul class="navbar-nav">
344
+
345
+ <li class="signedin-item py-4">
346
+
347
+ <%= link_to user_path(@user.id), class: "signedin-item text-decoration-none" do %>
348
+
349
+ <i class="far fa-user-circle mr-2 nav-items"></i> プロフィール
350
+
351
+ <% end %>
352
+
353
+ </li>
354
+
355
+ <li class="signedin-item">
356
+
357
+ <%= link_to "#", class: "signedin-item text-decoration-none" do %>
358
+
359
+ <i class="fas fa-map-marker-alt mr-2 nav-items"></i> マップ
360
+
361
+ <% end %>
362
+
363
+ </li>
364
+
365
+ <li class="signedin-item py-4">
366
+
367
+ <%= link_to users_path, class: "signedin-item text-decoration-none" do %>
368
+
369
+ <i class="fas fa-check mr-2 nav-items"></i> ユーザー一覧
370
+
371
+ <% end %>
372
+
373
+ </li>
374
+
375
+ <li class="signedin-item pb-2">
376
+
377
+ <%= link_to destroy_user_session_path, method: :delete, class: "signedin-item text-decoration-none" do %>
378
+
379
+ <span><i class="fas fa-power-off mr-2 nav-items"></i> ログアウト</span>
380
+
381
+ <% end %>
382
+
383
+ </li>
384
+
385
+ <% else %>
386
+
387
+ <li class="not-signedin-item pt-4 mb-3">
388
+
389
+ <%= link_to new_user_session_path, class: "not-signedin-item text-decoration-none" do %>
390
+
391
+ <i class="fas fa-sign-in-alt mr-2 nav-items"></i> ログイン
392
+
393
+ <% end %>
394
+
395
+ </li>
396
+
397
+ <li class="not-signedin-item mb-5">
398
+
399
+ <%= link_to users_guest_sign_in_path, method: :post, class: " not-signedin-item text-decoration-none" do %>
400
+
401
+ <i class="fas fa-sign-in-alt mr-2 nav-items"></i> 簡単ログイン
402
+
403
+ <% end %>
404
+
405
+ </li>
406
+
407
+ </ul>
408
+
409
+ <% end %>
410
+
411
+ </div>
412
+
413
+ </nav>
120
414
 
121
415
  ```
122
416