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>
ルーティングはどの様になっていますか?
情報不足ですいません。
ルーティングは下記になります。
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
どんなvalidationをかけていますか?
book_commentモデルには下記のバリデーションをかけてます。
class BookComment < ApplicationRecord
belongs_to :user
belongs_to :book
validates :comment, presence: true
end
これでvalidationがかからないのは、???
申し訳ない私ではわかりませんでした
回答1件
あなたの回答
tips
プレビュー