質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1572閲覧

railsでコメント機能を実装したいのですが、バリデーションがかからないです

dragonnext

総合スコア3

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/08/18 03:21

railsでアプリケーションにコメント機能を実装しており、投稿詳細画面にコメント入力フォームを追加しました。コメントはできるようになったのですが、未入力の際にエラーメッセージが表示されず、またurlも/books/:id/book_commentになってしまいます。
renderで他コントローラーのビューに設定してるのがいけないのでしょうか?ただ、render先のビューは表示されています。(urlはおかしいですが、、、)
どなたかアドバイス頂けますと幸いです。

book_comments_controller class BookCommentsController < ApplicationController def create @book = Book.find(params[:book_id]) comment = current_user.book_comments.new(book_comment_params) comment.book_id = @book.id if comment.save redirect_to book_path(book) else @book_new = Book.new @book_comment = BookComment.new render 'books/show' end end def destroy BookComment.find_by(id: params[:id], book_id: params[:book_id]).destroy redirect_to book_path(params[:book_id]) end private def book_comment_params params.require(:book_comment).permit(:comment) end end
books_controller class BooksController < ApplicationController def show @book = Book.find(params[:id]) @book_new = Book.new @book_comment = BookComment.new end def index @books = Book.all @book = Book.new end def create @book = Book.new(book_params) @book.user_id = current_user.id if @book.save redirect_to book_path(@book), notice: "You have created book successfully." else @books = Book.all render 'index' end end def edit @book = Book.find(params[:id]) if @book.user == current_user else redirect_to books_path end end def update @book = Book.find(params[:id]) if @book.update(book_params) redirect_to book_path(@book), notice: "You have updated book successfully." else render "edit" end end def destroy @book = Book.find(params[:id]) @book.destroy redirect_to books_path end private def book_params params.require(:book).permit(:title, :body) end end
books show.html.erb <div class="container"> <div class="row"> <div class="col-xs-3"> <h2>User info</h2> <%= render 'users/profile', user: @book.user %> <h2>New book</h2> <%= render 'books/newform', book: @book_new %> </div> <div class="col-xs-9"> <h2>Book detail</h2> <table class="table"> <tr> <td> <%= link_to user_path(@book.user.id) do %> <%= attachment_image_tag(@book.user, :profile_image, :fill, 100, 100, fallback: "no-image-mini.jpg") %><br> <%= @book.user.name %> <% end %> </td> <td><%= link_to @book.title, book_path(@book) %></td> <td><%= @book.body %></td> <td> <% if @book.favorited_by?(current_user) %> <%= link_to book_favorites_path(@book), method: :delete do %> <i class="fa fa-heart" aria-hidden="true" style="color: red;"></i> <%= @book.favorites.count %> <% end %> <% else%> <%= link_to book_favorites_path(@book), method: :post do %> <i class="fa fa-heart-o" aria-hidden="true"></i> <%= @book.favorites.count %> <% end %> <% end %> </td> <td> <span>コメント数:</span> <%= @book.book_comments.count %> </td> <% if @book.user == current_user %> <td><%= link_to "Edit", edit_book_path(@book), class: "btn-sm btn-success edit_book_#{@book.id}" %></td> <td><%= link_to "Destroy", book_path(@book), method: :delete, data: {confirm: "本当に削除してもよろしいですか?"}, class: "btn-sm btn-danger destroy_book_#{@book.id}" %></td> <% end %> </tr> </table> <table class="table"> <thead> <tr> <th></th> </tr> </thead> <tbody> <% @book.book_comments.each do |book_comment| %> <tr> <td> <%= link_to user_path(book_comment.user.id) do %> <%= attachment_image_tag(book_comment.user, :profile_image, :fill, 100,100, fallback: "no-image-mini.jpg") %> <br><%= book_comment.user.name %><br> <% end %> </td> <td> <%= book_comment.comment %> </td> <td> <% if book_comment.user == current_user %> <%= link_to 'Destroy', book_book_comment_path(book_comment.book, book_comment), method: :delete, data: {confirm: "本当に削除してもよろしいですか?"}, class: "btn-sm btn-danger" %> <% end %> </td> </tr> <% end %> </tbody> </table> <div class="book-comment-form"> <%= form_for [@book, @book_comment] do |f| %> <!-- エラーメッセージ表示--> <% if @book_comment.errors.any? %> <div id="error_explanation"> <h3><%= @book_comment.errors.count %>error prohibited this obj from being saved:</h3> <ul> <% @book_comment.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <!-- エラーメッセージ表示--> <%= f.text_area :comment, size: "80x5" %> <%= f.submit "送信" %> <% end %> </div> </div> </div> </div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2020/08/18 03:28

ルーティングはどの様になっていますか?
dragonnext

2020/08/18 03:30

情報不足ですいません。 ルーティングは下記になります。 Rails.application.routes.draw do root 'home#top' get 'home/about' devise_for :users resources :users, only: [:show,:index,:edit,:update] resources :books do resource :favorites, only: [:create, :destroy] resources :book_comments, only: [:create, :destroy] end end
winterboum

2020/08/18 05:08

どんなvalidationをかけていますか?
dragonnext

2020/08/18 05:11

book_commentモデルには下記のバリデーションをかけてます。 class BookComment < ApplicationRecord belongs_to :user belongs_to :book validates :comment, presence: true end
winterboum

2020/08/18 08:21

これでvalidationがかからないのは、??? 申し訳ない私ではわかりませんでした
guest

回答1

0

自己解決

book_comments controller createアクションでrender先に指定していたbooks/showのところで@book_comment = BookComment.newと変数を定義してしまっており、そのせいでエラー文が出なかったみたいです。
なのでそのコードを消したらエラー文が出ました。
ありがとうございました!

投稿2020/08/20 06:02

dragonnext

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問