teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

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

2020/05/14 12:58

投稿

mayok
mayok

スコア12

title CHANGED
File without changes
body CHANGED
@@ -206,6 +206,12 @@
206
206
  </div>
207
207
  </nav>
208
208
  ```
209
+ ```
210
+ (static_pages_controller.rb)
211
+ class StaticPagesController < ApplicationController
212
+ def home; end
213
+ end
214
+ ```
209
215
 
210
216
  ### 試したこと
211
217
 

2

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

2020/05/14 12:58

投稿

mayok
mayok

スコア12

title CHANGED
File without changes
body CHANGED
@@ -171,7 +171,7 @@
171
171
  <% if user_signed_in? %>
172
172
  <ul class="navbar-nav">
173
173
  <li class="signedin-item py-4">
174
- <%= link_to user_path(@user.id), class: "signedin-item text-decoration-none" do %>
174
+ <%= link_to user_path(current_user), class: "signedin-item text-decoration-none" do %>
175
175
  <i class="far fa-user-circle mr-2 nav-items"></i> プロフィール
176
176
  <% end %>
177
177
  </li>
@@ -215,7 +215,22 @@
215
215
  自分で調べた結果下記の二点もエラー解決に役立つ情報だと思います。どうコードを書き直せばよいか分かりませんでした。
216
216
 
217
217
  ・```発生しているエラーメッセージ```の項目でコードを載せた通り、パラメーターの値が"id" => "6"となるはずなのに"format" => "6"と出力されている。
218
+ **追記:その際の挙動は以下になります**
218
219
 
220
+ ```
221
+ Started GET "/user.6" for ::1 at 2020-05-14 21:33:41 +0900
222
+ (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
223
+ (0.5ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
224
+ Processing by UsersController#show as
225
+ Completed 500 in 20ms (ActiveRecord: 3.5ms | Allocations: 4928)
226
+
227
+
228
+
229
+ ActiveRecord::RecordNotFound (Couldn't find User without an ID):
230
+
231
+ app/controllers/users_controller.rb:70:in `set_user'
232
+ ```
233
+
219
234
  ・ログインユーザーのプロフィールを表示するためリンクを押すと、URLが```/user.1```となってしまう。
220
235
  正しくは```/user/1```となるべきではないのでしょうか?
221
236
 

1

controlller、viewを追加しました。

2020/05/14 12:46

投稿

mayok
mayok

スコア12

title CHANGED
File without changes
body CHANGED
@@ -45,20 +45,167 @@
45
45
 
46
46
  ```
47
47
  (users_controller.rb)
48
+ class UsersController < ApplicationController
49
+ before_action :set_user, only: [:show, :edit, :update, :destroy]
50
+
51
+ # GET /users
52
+ # GET /users.json
53
+ def index
54
+ @users = User.all
55
+ end
56
+
57
+ # GET /users/1
58
+ # GET /users/1.json
48
59
  def show
49
- @user = User.find(params[:id])
60
+ @user = User.find(params[:id])
50
61
  end
51
62
 
63
+ # GET /users/new
64
+ # def new
65
+ # @user = User.new
66
+ # end
67
+
68
+ # GET /users/1/edit
69
+ def edit
70
+ @user = User.find(params[:id])
71
+ end
72
+
73
+ # POST /users
74
+ # POST /users.json
75
+ def create
76
+ @user = User.new(user_params)
77
+ respond_to do |format|
78
+ if @user.save
79
+ format.html { redirect_to @user, notice: 'User was successfully created.' }
80
+ format.json { render :show, status: :created, location: @user }
81
+ else
82
+ format.html { render :new }
83
+ format.json { render json: @user.errors, status: :unprocessable_entity }
84
+ end
85
+ end
86
+ end
87
+
88
+ # PATCH/PUT /users/1
89
+ # PATCH/PUT /users/1.json
90
+ def update
91
+ respond_to do |format|
92
+ if @user.update(user_params)
93
+ format.html { redirect_to @user, notice: 'User was successfully updated.' }
94
+ format.json { render :show, status: :ok, location: @user }
95
+ else
96
+ format.html { render :edit }
97
+ format.json { render json: @user.errors, status: :unprocessable_entity }
98
+ end
99
+ end
100
+ end
101
+
102
+ # DELETE /users/1
103
+ # DELETE /users/1.json
104
+ def destroy
105
+ @user.destroy
106
+ respond_to do |format|
107
+ format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
108
+ format.json { head :no_content }
109
+ end
110
+ end
111
+
52
112
  private
53
113
 
114
+ # Use callbacks to share common setup or constraints between actions.
54
115
  def set_user
55
116
  @user = User.find(params[:id])
56
117
  end
118
+
119
+ # Never trust parameters from the scary internet, only allow the white list through.
120
+ def user_params
121
+ params.require(:user).permit(:name, :email)
122
+ end
123
+ end
124
+
57
125
  ```
58
126
 
59
127
  ```
60
- <%= link_to user_path(@user.id), class: "signedin-item text-decoration-none" do %>
128
+ (application.html.erb)
129
+ <!DOCTYPE html>
130
+ <html lang="ja">
131
+ <head>
132
+ <title><%= yield(:title) %> | Contrail</title>
133
+ <%= csrf_meta_tags %>
134
+ <%= csp_meta_tag %>
135
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
136
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
137
+ <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
138
+ <meta charset="utf-8">
139
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
140
+ <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" integrity="sha384-Bfad6CLCknfcloXFOyFnlgtENryhrpZCe29RTifKEixXQZ38WheV+i/6YWSzkz3V" crossorigin="anonymous">
141
+ </head>
142
+  <body>
143
+ <header>
144
+ <%= render "shared/header" %>
145
+ </header>
146
+ <main>
147
+ <%= render "shared/main" %>
148
+ <%= yield %>
149
+ </main>
150
+ <footer class="fixed-bottom text-center text-muted py-4">
151
+ <%= render "shared/footer" %>
152
+ </footer>
153
+
154
+ <%= render "shared/js" %>
155
+ </body>
156
+ </html>
61
157
  ```
158
+ ```
159
+ (application.html.erb内 _header.html.erb)
160
+ <nav class="navbar sticky-top mb-4">
161
+ <%=link_to "Contrail", root_path, class: "navbar-brand text-monospace" %>
162
+ <li>
163
+ <%=link_to class: "fa-heart" do %>
164
+ <i class="far fa-heart fa-lg"></i>
165
+ <% end %>
166
+ </li>
167
+ <button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="ナビゲーションの切替">
168
+ <i class="fas fa-bars fa-lg"></i>
169
+ </button>
170
+ <div class="collapse navbar-collapse text-left" id="navbarNavAltMarkup">
171
+ <% if user_signed_in? %>
172
+ <ul class="navbar-nav">
173
+ <li class="signedin-item py-4">
174
+ <%= link_to user_path(@user.id), class: "signedin-item text-decoration-none" do %>
175
+ <i class="far fa-user-circle mr-2 nav-items"></i> プロフィール
176
+ <% end %>
177
+ </li>
178
+ <li class="signedin-item">
179
+ <%= link_to "#", class: "signedin-item text-decoration-none" do %>
180
+ <i class="fas fa-map-marker-alt mr-2 nav-items"></i> マップ
181
+ <% end %>
182
+ </li>
183
+ <li class="signedin-item py-4">
184
+ <%= link_to users_path, class: "signedin-item text-decoration-none" do %>
185
+ <i class="fas fa-check mr-2 nav-items"></i> ユーザー一覧
186
+ <% end %>
187
+ </li>
188
+ <li class="signedin-item pb-2">
189
+ <%= link_to destroy_user_session_path, method: :delete, class: "signedin-item text-decoration-none" do %>
190
+ <span><i class="fas fa-power-off mr-2 nav-items"></i> ログアウト</span>
191
+ <% end %>
192
+ </li>
193
+ <% else %>
194
+ <li class="not-signedin-item pt-4 mb-3">
195
+ <%= link_to new_user_session_path, class: "not-signedin-item text-decoration-none" do %>
196
+ <i class="fas fa-sign-in-alt mr-2 nav-items"></i> ログイン
197
+ <% end %>
198
+ </li>
199
+ <li class="not-signedin-item mb-5">
200
+ <%= link_to users_guest_sign_in_path, method: :post, class: " not-signedin-item text-decoration-none" do %>
201
+ <i class="fas fa-sign-in-alt mr-2 nav-items"></i> 簡単ログイン
202
+ <% end %>
203
+ </li>
204
+ </ul>
205
+ <% end %>
206
+ </div>
207
+ </nav>
208
+ ```
62
209
 
63
210
  ### 試したこと
64
211