前提
プログラミング初学者です。
本の感想を投稿できるサイトを作っています。投稿に対してのコメントを実装中なのですが、コメントを投稿しても同じページにリダイレクトされるだけでコメントは表示されず、エラーも表示されないので原因が分からないでいます。ログを見ても問題なく投稿されている感じです。
実現したいこと
コメントを表示させたい
発生している問題・エラーメッセージ
エラーメッセージはありません
該当のソースコード
books/show.html.erb
1 <div> 2 <% @book.book_comments.each do |book_comment| %> 3 <% binding.pry %> <%#反応せず%> 4 <%= image_tag book_comment.user.get_profile_image(100,100) %> 5 <%= book_comment.user.name %> 6 <%= book_comment.created_at.strftime('%Y/%m/%d') %><%= book_comment.comment %> 7 <% if book.user == current_user %> 8 <%= link_to "削除", book_book_comment_path(book_comment.book, book_comment), method: :delete %> 9 <% end %> 10 <% end %> 11 </div> 12 <div> 13 <%= form_with model: [@book, @book_comment] do |f| %> 14 <%= f.text_area :comment, rows: '5', placeholder: "コメントをここに" %> 15 <%= f.submit "送信" %> 16 <% end %> 17 </div> 18 </div>
book_comments_controller.rb
1class BookCommentsController < ApplicationController 2 def create 3 book = Book.find(params[:book_id]) 4 comment = current_user.book_comments.new(book_comment_params) 5 comment.book_id = book.id 6 comment.save 7 redirect_to book_path(book) 8 end 9 10 def destroy 11 BookComment.find(params[:id]).destroy 12 redirect_to book_path(params[:book_id]) 13 end 14 15 private 16 17 def book_comment_params 18 params.require(:book_comment).permit(:comment) 19 end 20end
books_controller.rb
1class BooksController < ApplicationController 2before_action :ensure_correct_user, only: [:update,:edit] 3 4 def show 5 @book = Book.find(params[:id]) 6 @user = @book.user 7 @book_comment = BookComment.new 8 end
log
1Started POST "/books/5/book_comments" for 138.199.22.230 at 2023-01-02 11:13:45 +0000 2Cannot render console from 138.199.22.230! Allowed networks: 127.0.0.0/127.255.255.255, ::1 3Processing by BookCommentsController#create as HTML 4 Parameters: {"authenticity_token"=>"[FILTERED]", "book_comment"=>{"comment"=>"こめんと1"}, "commit"=>"送信", "book_id"=>"5"} 5 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] 6 Book Load (0.1ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] 7 ↳ app/controllers/book_comments_controller.rb:3:in `create' 8Redirected to https://57d4bb8293114d7aa8fbd4aa62dbad6d.vfs.cloud9.ap-northeast-1.amazonaws.com/books/5 9Completed 302 Found in 9ms (ActiveRecord: 0.3ms | Allocations: 2777) 10 11 12Started GET "/books/5" for 138.199.22.230 at 2023-01-02 11:13:45 +0000 13Cannot render console from 138.199.22.230! Allowed networks: 127.0.0.0/127.255.255.255, ::1 14Processing by BooksController#show as HTML 15 Parameters: {"id"=>"5"} 16 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] 17 Book Load (0.5ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] 18 ↳ app/controllers/books_controller.rb:5:in `show' 19 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 20 ↳ app/controllers/books_controller.rb:6:in `show' 21 Rendering layout layouts/application.html.erb 22 Rendering books/show.html.erb within layouts/application 23 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]] 24 ↳ app/models/user.rb:19:in `get_profile_image' 25 Rendered users/_info.html.erb (Duration: 5.8ms | Allocations: 1686) 26 Rendered books/_form.html.erb (Duration: 1.3ms | Allocations: 666) 27 (0.2ms) SELECT COUNT(*) FROM "book_comments" WHERE "book_comments"."book_id" = ? [["book_id", 5]] 28 ↳ app/views/books/show.html.erb:25 29 BookComment Load (0.1ms) SELECT "book_comments".* FROM "book_comments" WHERE "book_comments"."book_id" = ? [["book_id", 5]] 30 ↳ app/views/books/show.html.erb:30 31 Rendered books/show.html.erb within layouts/application (Duration: 16.7ms | Allocations: 4894) 32[Webpacker] Everything's up-to-date. Nothing to do 33 Rendered layouts/_header.html.erb (Duration: 0.6ms | Allocations: 239) 34 Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 38) 35 Rendered layout layouts/application.html.erb (Duration: 23.2ms | Allocations: 7005) 36Completed 200 OK in 34ms (Views: 24.4ms | ActiveRecord: 1.2ms | Allocations: 9918)
models/book.rb
1class Book < ApplicationRecord 2 belongs_to :user 3 validates :title,presence:true 4 validates :body,presence:true,length:{maximum:200} 5 has_many :favorites,dependent: :destroy 6 has_many :book_comments,dependent: :destroy 7 8 9 def favorited_by?(user) 10 favorites.exists?(user_id: user.id) 11 end 12end 13
models/book_comment.rb
1class BookComment < ApplicationRecord 2 belongs_to :user 3 belongs_to :book_comment 4end 5
models/user.rb
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :books 8 has_one_attached :profile_image 9 has_many :book_comments,dependent: :destroy 10 has_many :favorites,dependent: :destroy 11 12 13 validates :name, length: { in: 2..20 } 14 validates :name, uniqueness: true 15 validates :introduction,length: { maximum:50 } 16 17 18 def get_profile_image 19 (profile_image.attached?) ? profile_image : 'no_image.jpg' 20 end 21end 22
試したこと
https://teratail.com/questions/321939
を参考にbinding.pryを入れてみるが反応なし
回答1件