質問編集履歴
1
写真の貼り付けではなく、<code>を使って記載
title
CHANGED
File without changes
|
body
CHANGED
@@ -28,38 +28,257 @@
|
|
28
28
|
下記のようなエラーが出てしまい、解決策が分からないため、ご教授いただけると幸いです。
|
29
29
|
|
30
30
|
### 発生している問題・エラーメッセージ
|
31
|
-
|
31
|
+
```
|
32
|
+
コード
|
32
|
-
|
33
|
+
NoMethodError in Books#create
|
34
|
+
Showing /home/ec2-user/environment/Bookers2/app/views/users/_user_show.html.erb where line #7 raised:
|
33
35
|
|
36
|
+
undefined method `name' for nil:NilClass
|
37
|
+
Extracted source (around line #7):
|
38
|
+
5 <tbody>
|
39
|
+
6 <tr>
|
40
|
+
7 <th>name</th><th><%= user.name %></th>
|
41
|
+
8 </tr>
|
42
|
+
9 <tr>
|
43
|
+
10 <th>introduction</th><th><%= user.introduction %></th>
|
34
44
|
|
45
|
+
|
46
|
+
|
35
|
-
|
47
|
+
```
|
36
48
|
|
37
49
|
|
50
|
+
### 該当のソースコード
|
51
|
+
```
|
38
52
|
ソースコード
|
39
|
-
・models/book.rb
|
40
|
-

|
41
53
|
|
54
|
+
[ブックモデル]
|
42
|
-
|
55
|
+
class Book < ApplicationRecord
|
43
|
-

|
44
56
|
|
45
|
-
|
57
|
+
belongs_to :user
|
46
|
-

|
47
|
-

|
48
58
|
|
59
|
+
validates :title, presence: true
|
49
|
-
|
60
|
+
validates :body, presence: true
|
50
|
-
|
61
|
+
validates :body, length: { maximum: 200 }
|
51
|
-

|
52
62
|
|
53
|
-
|
63
|
+
end
|
54
|
-

|
55
64
|
|
56
|
-
・_user_show.html(部分テンプレートです)
|
57
|
-

|
58
65
|
|
66
|
+
[ユーザーモデル]
|
67
|
+
class User < ApplicationRecord
|
68
|
+
# Include default devise modules. Others available are:
|
59
|
-
|
69
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
60
|
-
|
70
|
+
devise :database_authenticatable, :registerable,
|
71
|
+
:recoverable, :rememberable, :validatable
|
61
72
|
|
73
|
+
has_many :books, dependent: :destroy
|
74
|
+
attachment :profile_image
|
62
75
|
|
76
|
+
|
77
|
+
validates :name, uniqueness: true
|
78
|
+
validates :name, length: { in: 2..20 }
|
79
|
+
validates :introduction, length: { maximum: 50 }
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
[ブックコントローラー]
|
84
|
+
class BooksController < ApplicationController
|
85
|
+
|
86
|
+
before_action :authenticate_user!
|
87
|
+
|
88
|
+
before_action :ensure_correct_user, only:[:edit]
|
89
|
+
|
90
|
+
def new
|
91
|
+
@book = Book.new
|
92
|
+
end
|
93
|
+
|
94
|
+
def create
|
95
|
+
@book = Book.new(book_params)
|
96
|
+
@book.user_id = current_user.id
|
97
|
+
if @book.save
|
98
|
+
flash[:notice] = "You have created book successfully."
|
99
|
+
redirect_to book_path(@book)
|
100
|
+
else
|
101
|
+
render :index
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def index
|
106
|
+
@books = Book.all
|
107
|
+
@user = current_user
|
108
|
+
@book = Book.new
|
109
|
+
end
|
110
|
+
|
111
|
+
def show
|
112
|
+
@book = Book.find(params[:id])
|
113
|
+
@book_new = Book.new
|
114
|
+
end
|
115
|
+
|
116
|
+
def edit
|
117
|
+
@book = Book.find(params[:id])
|
118
|
+
end
|
119
|
+
|
120
|
+
def update
|
121
|
+
@book = Book.find(params[:id])
|
122
|
+
@book.user_id = current_user.id
|
123
|
+
if @book.update(book_params)
|
124
|
+
flash[:notice] = "You have updated book successfully."
|
125
|
+
redirect_to book_path(@book)
|
126
|
+
else
|
127
|
+
render :edit
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def destroy
|
132
|
+
@book = Book.find(params[:id])
|
133
|
+
@book.destroy
|
134
|
+
redirect_to books_path
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def book_params
|
140
|
+
params.require(:book).permit(:title, :body)
|
141
|
+
end
|
142
|
+
|
143
|
+
def ensure_correct_user
|
144
|
+
@book = Book.find(params[:id])
|
145
|
+
unless @book.user == current_user
|
146
|
+
redirect_to user_path
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
[ユーザーコントローラー]
|
154
|
+
class UsersController < ApplicationController
|
155
|
+
|
156
|
+
before_action :authenticate_user!
|
157
|
+
|
158
|
+
before_action :ensure_correct_user, only:[:edit, :show]
|
159
|
+
|
160
|
+
def show
|
161
|
+
@user = User.find(params[:id])
|
162
|
+
@book = Book.new
|
163
|
+
@books = @user.books
|
164
|
+
end
|
165
|
+
|
166
|
+
def new
|
167
|
+
@book = Book.new
|
168
|
+
end
|
169
|
+
|
170
|
+
def create
|
171
|
+
@book = Book.new(book_params)
|
172
|
+
@book.user_id = current_user.id
|
173
|
+
if @book.save
|
174
|
+
redirect_to book_path(@book)
|
175
|
+
else
|
176
|
+
render :index
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def index
|
181
|
+
@users = User.all
|
182
|
+
end
|
183
|
+
|
184
|
+
def edit
|
185
|
+
@user = User.find(params[:id])
|
186
|
+
end
|
187
|
+
|
188
|
+
def update
|
189
|
+
@user = User.find(params[:id])
|
190
|
+
@user = current_user
|
191
|
+
if @user.update(user_params)
|
192
|
+
flash[:notice] = "You have updated user succeessfully."
|
193
|
+
redirect_to user_path(@user)
|
194
|
+
else
|
195
|
+
render :edit
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
private
|
200
|
+
|
201
|
+
def user_params
|
202
|
+
params.require(:user).permit(:name, :introduction, :profile_image_id)
|
203
|
+
end
|
204
|
+
|
205
|
+
def ensure_correct_user
|
206
|
+
@user = User.find(params[:id])
|
207
|
+
unless @user == current_user
|
208
|
+
redirect_to user_path(current_user.id)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
[部分テンプレート、_book_new.html]
|
215
|
+
<h2>New book</h2>
|
216
|
+
|
217
|
+
<%= form_with model: book, local:true do |f| %>
|
218
|
+
<div class="form-group">
|
219
|
+
<%= f.label :title, 'Title' %>
|
220
|
+
<%= f.text_field :title, class: 'form-control' %>
|
221
|
+
</div>
|
222
|
+
|
223
|
+
<div class="form-group">
|
224
|
+
<%= f.label :body, 'Opinion' %>
|
225
|
+
<%= f.text_area :body, class: 'form-control' %>
|
226
|
+
</div>
|
227
|
+
|
228
|
+
<div><%= f.submit 'Create Book', class: 'btn btn-success' %></div>
|
229
|
+
<% end %>
|
230
|
+
|
231
|
+
|
232
|
+
[部分テンプレート、_user_show.html]
|
233
|
+
<h2>User info</h2>
|
234
|
+
<%= attachment_image_tag user, :profile_image, size: "60x60", farmat: 'jpg', fallback: "no_image.jpg" %>
|
235
|
+
<table class="table">
|
236
|
+
<tbody>
|
237
|
+
<tr>
|
238
|
+
<th>name</th><th><%= user.name %></th>
|
239
|
+
</tr>
|
240
|
+
<tr>
|
241
|
+
<th>introduction</th><th><%= user.introduction %></th>
|
242
|
+
</tr>
|
243
|
+
</tbody>
|
244
|
+
</table>
|
245
|
+
|
246
|
+
<div class="row">
|
247
|
+
<%= link_to edit_user_path(user), class: "btn btn-outline-dark btn-block" do %>
|
248
|
+
<span class="fas fa-user-cog"></span>
|
249
|
+
<% end %>
|
250
|
+
</div>
|
251
|
+
|
252
|
+
|
253
|
+
[books/index.html(投稿に失敗した時は本の一覧画面に返るようにしたいので、ここに<% if @book.errors.any? %>を書きました)]
|
254
|
+
<div class="container">
|
255
|
+
<div class="row">
|
256
|
+
<% if @book.errors.any? %>
|
257
|
+
<%= @book.errors.count %>error prohibited this book from being saved:<br>
|
258
|
+
<% @book.errors.full_messages.each do |message| %>
|
259
|
+
<%= message %>
|
260
|
+
<% end %>
|
261
|
+
<% end %>
|
262
|
+
<div class="col-md-3">
|
263
|
+
<%= render 'users/user_show', user: @user %>
|
264
|
+
<%= render 'books/book_new', book: @book %>
|
265
|
+
</div>
|
266
|
+
<div class="col-md-8 offset-md-1 pt-3">
|
267
|
+
<%= render 'books/book_index', books: @books %>
|
268
|
+
</div>
|
269
|
+
</div>
|
270
|
+
```
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
63
282
|
### 試したこと
|
64
283
|
|
65
284
|
コントローラなど見返したのですが、どのように直せば良いか分からなかったです。
|
@@ -67,4 +286,7 @@
|
|
67
286
|
|
68
287
|
### 補足情報(FW/ツールのバージョンなど)
|
69
288
|
|
70
|
-
ここにより詳細な情報を記載してください。
|
289
|
+
ここにより詳細な情報を記載してください。
|
290
|
+
ご指摘ありがとうございます。
|
291
|
+
コピペして訂正しました、
|
292
|
+
初めての質問で見辛い点もあるかと思いますが、よろしくお願いいたします。
|