前提・実現したいこと
rails6 でSNSを作成中、投稿に対するコメント機能を実装中です。
コメントコントローラーに以下のように記述し、books(投稿)のshowページにコメントの投稿フォームを作成しています。しかしコメントを送信しようとすると必ず@comment.save が失敗してflashの”失敗”が表示されてしまいます。
class CommentsController < ApplicationController def create @comment = current_user.comments.new(comment_params) if @comment.save flash[:success] = "成功" redirect_back(fallback_location: root_path) else flash[:danger] = "失敗" redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
ログの記載
Started POST "/books/303/comments" for 126.36.208.218 at 2020-09-06 14:41:42 +0000 Cannot render console from 126.36.208.218! Allowed networks: 127.0.0.0/127.255.255.255, ::1 (0.1ms) SELECT sqlite_version(*) Processing by CommentsController#create as HTML Parameters: {"authenticity_token"=>"q+QVqM97YBjBQSNBKBx4y0pcifU5iXCP805YIwLbF+s5o1GgAWEf7hT7/xxmWxXbX3WmD0oGzQpvRzFVuNG8LQ==", "user_id"=>"1", "comment"=>{"content"=>"こめ"}, "commit"=>"返信", "book_id"=>"303"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:8:in `current_user' Redirected to https://12dac388e20f4c1f9f6629019404d368.vfs.cloud9.us-east-2.amazonaws.com/books/303 Completed 302 Found in 33ms (ActiveRecord: 1.5ms | Allocations: 20459)
該当のソースコード
routes.rb
Rails.application.routes.draw do root 'static_pages#home' get '/home', to: 'static_pages#home' get '/help', to: 'static_pages#help' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' resources :users do member do get :following, :followers end end resources :books, only: [:show, :create, :destroy] do resources :likes, only: [:create, :destroy] resources :comments, only: [:create] end resources :relationships, only: [:create, :destroy] end
bookモデル
belongs_to :user has_many :likes has_many :liked_user, through: :likes, source: :user has_many :comments has_many :commented_user, through: :comments, source: :user
commentモデル
class Comment < ApplicationRecord belongs_to :user belongs_to :book validates :content, presence: true end
booksコントローラー
def show @user = User.find(params[:id]) @books = @user.books.paginate(page: params[:page]) @like = Like.new end
commentコントローラー
class CommentsController < ApplicationController def create @comment = current_user.comments.new(comment_params) if @comment.save flash[:success] = "成功" redirect_back(fallback_location: root_path) else flash[:danger] = "失敗" redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
_comment_form.html.erb
<div> <%= form_with(model: [@book, @comment], local: true) do |f| %> <p><%= hidden_field_tag :user_id, current_user.id %></p> <%= f.text_area :content, placeholder: "コメント(30文字以内)", class: "comment_form" %> <%= f.submit "返信", class: "btn btn-primary" %> <% end %> </div>
追記
<div> <%= form_with(model: [@book, @comment], local: true) do |f| %> #エラーメッセージを表示 <%= render 'shared/error_messages', object: f.object %> <%= f.text_area :content, placeholder: "コメント(30文字以内)", class: "comment_form" %> <%= f.submit "返信", class: "btn" %> <% end %> </div>_comment_form.html.erb
shared/_error_messages.html.erb
<% if object.errors.any? %> <div id="error_explanation"> <div class="alert alert-danger"> エラー<%= object.errors.count %>件: <ul> <% object.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> </div> <% end %>
としたのですが、@comment.saveをした後にエラーメッセージが何も表示されません。。
回答1件
あなたの回答
tips
プレビュー