質問編集履歴
2
modelファイルを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -103,8 +103,56 @@
|
|
103
103
|
Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 38)
|
104
104
|
Rendered layout layouts/application.html.erb (Duration: 23.2ms | Allocations: 7005)
|
105
105
|
Completed 200 OK in 34ms (Views: 24.4ms | ActiveRecord: 1.2ms | Allocations: 9918)
|
106
|
+
```
|
107
|
+
|
108
|
+
```models/book.rb
|
109
|
+
class Book < ApplicationRecord
|
110
|
+
belongs_to :user
|
111
|
+
validates :title,presence:true
|
112
|
+
validates :body,presence:true,length:{maximum:200}
|
113
|
+
has_many :favorites,dependent: :destroy
|
114
|
+
has_many :book_comments,dependent: :destroy
|
115
|
+
|
116
|
+
|
117
|
+
def favorited_by?(user)
|
118
|
+
favorites.exists?(user_id: user.id)
|
119
|
+
end
|
120
|
+
end
|
106
121
|
|
107
122
|
```
|
123
|
+
```models/book_comment.rb
|
124
|
+
class BookComment < ApplicationRecord
|
125
|
+
belongs_to :user
|
126
|
+
belongs_to :book_comment
|
127
|
+
end
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
```models/user.rb
|
132
|
+
class User < ApplicationRecord
|
133
|
+
# Include default devise modules. Others available are:
|
134
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
135
|
+
devise :database_authenticatable, :registerable,
|
136
|
+
:recoverable, :rememberable, :validatable
|
137
|
+
|
138
|
+
has_many :books
|
139
|
+
has_one_attached :profile_image
|
140
|
+
has_many :book_comments,dependent: :destroy
|
141
|
+
has_many :favorites,dependent: :destroy
|
142
|
+
|
143
|
+
|
144
|
+
validates :name, length: { in: 2..20 }
|
145
|
+
validates :name, uniqueness: true
|
146
|
+
validates :introduction,length: { maximum:50 }
|
147
|
+
|
148
|
+
|
149
|
+
def get_profile_image
|
150
|
+
(profile_image.attached?) ? profile_image : 'no_image.jpg'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
```
|
155
|
+
|
108
156
|
|
109
157
|
|
110
158
|
### 試したこと
|
1
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
### 前提
|
2
|
+
プログラミング初学者です。
|
2
3
|
本の感想を投稿できるサイトを作っています。投稿に対してのコメントを実装中なのですが、コメントを投稿しても同じページにリダイレクトされるだけでコメントは表示されず、エラーも表示されないので原因が分からないでいます。ログを見ても問題なく投稿されている感じです。
|
3
4
|
### 実現したいこと
|
4
|
-
|
5
|
-
ここに実現したいことを箇条書きで書いてください。
|
6
5
|
コメントを表示させたい
|
7
6
|
|
8
7
|
### 発生している問題・エラーメッセージ
|
@@ -13,29 +12,10 @@
|
|
13
12
|
|
14
13
|
### 該当のソースコード
|
15
14
|
|
16
|
-
```show.html.erb
|
15
|
+
```books/show.html.erb
|
17
|
-
<div class='col-md-8 offset-md-1'>
|
18
|
-
<h2>Book detail</h2>
|
19
|
-
<table class='table'>
|
20
|
-
<tr>
|
21
|
-
<td><%= link_to(@book.user) do %>
|
22
|
-
<%= image_tag @book.user.get_profile_image, size:"100x100" %><br>
|
23
|
-
<%= @book.user.name %>
|
24
|
-
<% end %>
|
25
|
-
</td>
|
26
|
-
<td><%= link_to @book.title, @book %></td>
|
27
|
-
<td><%= @book.body %></td>
|
28
|
-
<% if @book.user == current_user %>
|
29
|
-
<td><%= link_to 'Edit', edit_book_path(@book), class: "btn btn-sm btn-success" %></td>
|
30
|
-
<td><%= link_to 'Destroy', @book, method: :delete, data: { confirm: '本当に消しますか?' }, class: "btn btn-sm btn-danger"%></td>
|
31
|
-
<% end %>
|
32
|
-
<div>
|
33
|
-
<td>コメント数:<%= @book.book_comments.count %></td>
|
34
|
-
</div>
|
35
|
-
</tr>
|
36
|
-
</table>
|
37
16
|
<div>
|
38
17
|
<% @book.book_comments.each do |book_comment| %>
|
18
|
+
<% binding.pry %> <%#反応せず%>
|
39
19
|
<%= image_tag book_comment.user.get_profile_image(100,100) %>
|
40
20
|
<%= book_comment.user.name %>
|
41
21
|
<%= book_comment.created_at.strftime('%Y/%m/%d') %><%= book_comment.comment %>
|
@@ -53,13 +33,83 @@
|
|
53
33
|
</div>
|
54
34
|
```
|
55
35
|
|
36
|
+
```book_comments_controller.rb
|
37
|
+
class BookCommentsController < ApplicationController
|
38
|
+
def create
|
39
|
+
book = Book.find(params[:book_id])
|
40
|
+
comment = current_user.book_comments.new(book_comment_params)
|
41
|
+
comment.book_id = book.id
|
42
|
+
comment.save
|
43
|
+
redirect_to book_path(book)
|
44
|
+
end
|
45
|
+
|
46
|
+
def destroy
|
47
|
+
BookComment.find(params[:id]).destroy
|
48
|
+
redirect_to book_path(params[:book_id])
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def book_comment_params
|
54
|
+
params.require(:book_comment).permit(:comment)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
```books_controller.rb
|
60
|
+
class BooksController < ApplicationController
|
61
|
+
before_action :ensure_correct_user, only: [:update,:edit]
|
62
|
+
|
63
|
+
def show
|
64
|
+
@book = Book.find(params[:id])
|
65
|
+
@user = @book.user
|
66
|
+
@book_comment = BookComment.new
|
67
|
+
end
|
68
|
+
```
|
69
|
+
```log
|
70
|
+
Started POST "/books/5/book_comments" for 138.199.22.230 at 2023-01-02 11:13:45 +0000
|
71
|
+
Cannot render console from 138.199.22.230! Allowed networks: 127.0.0.0/127.255.255.255, ::1
|
72
|
+
Processing by BookCommentsController#create as HTML
|
73
|
+
Parameters: {"authenticity_token"=>"[FILTERED]", "book_comment"=>{"comment"=>"こめんと1"}, "commit"=>"送信", "book_id"=>"5"}
|
74
|
+
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
|
75
|
+
Book Load (0.1ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
|
76
|
+
↳ app/controllers/book_comments_controller.rb:3:in `create'
|
77
|
+
Redirected to https://57d4bb8293114d7aa8fbd4aa62dbad6d.vfs.cloud9.ap-northeast-1.amazonaws.com/books/5
|
78
|
+
Completed 302 Found in 9ms (ActiveRecord: 0.3ms | Allocations: 2777)
|
79
|
+
|
80
|
+
|
81
|
+
Started GET "/books/5" for 138.199.22.230 at 2023-01-02 11:13:45 +0000
|
82
|
+
Cannot render console from 138.199.22.230! Allowed networks: 127.0.0.0/127.255.255.255, ::1
|
83
|
+
Processing by BooksController#show as HTML
|
84
|
+
Parameters: {"id"=>"5"}
|
85
|
+
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
|
86
|
+
Book Load (0.5ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
|
87
|
+
↳ app/controllers/books_controller.rb:5:in `show'
|
88
|
+
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
|
89
|
+
↳ app/controllers/books_controller.rb:6:in `show'
|
90
|
+
Rendering layout layouts/application.html.erb
|
91
|
+
Rendering books/show.html.erb within layouts/application
|
92
|
+
ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 1], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]]
|
93
|
+
↳ app/models/user.rb:19:in `get_profile_image'
|
94
|
+
Rendered users/_info.html.erb (Duration: 5.8ms | Allocations: 1686)
|
95
|
+
Rendered books/_form.html.erb (Duration: 1.3ms | Allocations: 666)
|
96
|
+
(0.2ms) SELECT COUNT(*) FROM "book_comments" WHERE "book_comments"."book_id" = ? [["book_id", 5]]
|
97
|
+
↳ app/views/books/show.html.erb:25
|
98
|
+
BookComment Load (0.1ms) SELECT "book_comments".* FROM "book_comments" WHERE "book_comments"."book_id" = ? [["book_id", 5]]
|
99
|
+
↳ app/views/books/show.html.erb:30
|
100
|
+
Rendered books/show.html.erb within layouts/application (Duration: 16.7ms | Allocations: 4894)
|
101
|
+
[Webpacker] Everything's up-to-date. Nothing to do
|
102
|
+
Rendered layouts/_header.html.erb (Duration: 0.6ms | Allocations: 239)
|
103
|
+
Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 38)
|
104
|
+
Rendered layout layouts/application.html.erb (Duration: 23.2ms | Allocations: 7005)
|
105
|
+
Completed 200 OK in 34ms (Views: 24.4ms | ActiveRecord: 1.2ms | Allocations: 9918)
|
106
|
+
|
107
|
+
```
|
56
108
|
|
57
109
|
|
58
110
|
### 試したこと
|
59
111
|
|
112
|
+
https://teratail.com/questions/321939
|
60
|
-
|
113
|
+
を参考にbinding.pryを入れてみるが反応なし
|
61
114
|
|
62
|
-
### 補足情報(FW/ツールのバージョンなど)
|
63
115
|
|
64
|
-
ここにより詳細な情報を記載してください。
|
65
|
-
|