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

質問編集履歴

4

文章を編集しました。

2019/12/14 10:54

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -117,6 +117,15 @@
117
117
  end
118
118
 
119
119
  private
120
+
121
+ def user_params
122
+ params.require(:user).permit(:name, :introduction, :profile_image_id)
123
+ end
124
+
125
+ def book_params
126
+ params.require(:book).permit(:title, :body)
127
+ end
128
+ end
120
129
  ```
121
130
 
122
131
  また、user/show.html.erbにも<%= link_to edit_user_path(@user.id), class: "btn btn-default" do %>の記述がありますが、問題なく機能しています。

3

コードを追記しました。

2019/12/14 10:54

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -138,4 +138,74 @@
138
138
  16 @user = User.find(params[:id])
139
139
  17 @book = Book.find(params[:id])
140
140
  18 end
141
- ```
141
+ ```
142
+
143
+ 【追記2】
144
+ ```
145
+ user/show.html.erb
146
+
147
+ <%= render 'shared/header' %>
148
+
149
+ <div class="top">
150
+
151
+ <div class="container">
152
+ <div class="row">
153
+ <div class="col-lg-3">
154
+
155
+ <p>User info</p>
156
+ <%= attachment_image_tag @user, :profile_image, :fill, 150, 150, format: 'jpeg', fallback: "no_image.jpg", size:'150x150' %>
157
+ <table class="table">
158
+ <tr>
159
+    <th>name</th>
160
+    <th><%= current_user.name %></th>
161
+   </tr>
162
+   <tr>
163
+    <th>introduction</th>
164
+    <th><%= current_user.introduction %></th>
165
+    </tr>
166
+ </table>
167
+ <%= link_to edit_user_path(@user.id), class: "btn btn-default" do %>
168
+ <i class="fa fa-wrench"></i>
169
+ <% end %>
170
+ <p>New book</p>
171
+ <%= form_for @book do |f| %>
172
+ <p>Title</p>
173
+ <%= f.text_field :title %>
174
+ <p>Opinion</p>
175
+ <%= f.text_area :body %>
176
+ <%= f.submit 'Create Book' %>
177
+ <% end %>
178
+ </div>
179
+
180
+ <div class="col-lg-9">
181
+ <p>Books</p>
182
+ <table class="table table-hover">
183
+
184
+ <thead>
185
+ <tr>
186
+ <th></th>
187
+ <th>Title</th>
188
+ <th>Opinion</th>
189
+ <th></th>
190
+ </tr>
191
+ </thead>
192
+ <% @books.each do |book| %>
193
+ <tbody>
194
+ <tr>
195
+ <td><%= attachment_image_tag @user, :profile_image, :fill, 50, 50, format: 'jpeg', fallback: "no_image.jpg", size:'50x50' %></td>
196
+ <td><%= link_to book_path(book.id) do %><%= book.title %><% end %></td>
197
+ <td><%= book.body %></td>
198
+ </tr>
199
+ </tbody>
200
+ <% end %>
201
+ </table>
202
+ </div>
203
+
204
+ </div>
205
+ </div>
206
+
207
+ </div>
208
+
209
+ <%= render 'shared/footer' %>
210
+ ```
211
+ <td><%= link_to book_path(book.id) do %><%= book.title %><% end %></td>でbook/show.html.erbに移動します。

2

文章を編集しました。

2019/12/14 10:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -126,6 +126,7 @@
126
126
  books_controller.rbのshowに@userを追記したところ以下のようなエラーが発生しました。
127
127
  エラー文の「Couldn't find User with 'id'=13」の13は投稿した本のidで、投稿したユーザーのidは2です。
128
128
  表示したいページはidが13の投稿の詳細画面です。
129
+ privateにuserのストロングパラメータを追加しても解決できませんでした。
129
130
  userが誤ってbookのidを取得しようとしているということでしょうか?
130
131
 
131
132
  ```

1

質問を追記しました。

2019/12/13 13:41

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -120,4 +120,21 @@
120
120
  ```
121
121
 
122
122
  また、user/show.html.erbにも<%= link_to edit_user_path(@user.id), class: "btn btn-default" do %>の記述がありますが、問題なく機能しています。
123
- コントローラの記述の仕方によるのでしょうか?
123
+ コントローラの記述の仕方によるのでしょうか?
124
+
125
+ 【追記】
126
+ books_controller.rbのshowに@userを追記したところ以下のようなエラーが発生しました。
127
+ エラー文の「Couldn't find User with 'id'=13」の13は投稿した本のidで、投稿したユーザーのidは2です。
128
+ 表示したいページはidが13の投稿の詳細画面です。
129
+ userが誤ってbookのidを取得しようとしているということでしょうか?
130
+
131
+ ```
132
+ ActiveRecord::RecordNotFound in BooksController#show
133
+ Couldn't find User with 'id'=13
134
+ Extracted source (around line #16):
135
+
136
+ 15 def show
137
+ 16 @user = User.find(params[:id])
138
+ 17 @book = Book.find(params[:id])
139
+ 18 end
140
+ ```