Controller to create a comment:
class CommentsController < ApplicationController def new @comment = Comment.new end def create @comment = Comment.new(comments_params) @comment = current_user.comments.create if @comment.save flash[:success] = "New comment was successufly added" redirect_to film_sessions_path else render 'new' end end private def comments_params params.require(:comment).permit(:body) end end The form:
<div class="container"> <div class="row"> <% if @comment.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comments from being saved:</h2> <ul> <% @comment.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="well bs-component comments"> <%= form_for @comment do |f| %> <div class="form-group is-empty"> <label class="col-md-2 control-label">Comment:</label> <div class="col-md-10"> <%= f.text_area :body, class:"form-control" %> </div> </div> <div class="form-group"> <div class="col-md-10 col-md-offset-2"> <%= link_to 'Back', film_sessions_path, class:"btn btn-raised btn-default" %> <%= f.submit class:"btn btn-raised btn-primary m20" %> </div> </div> <% end %> <div class="navi-btns"> <br> </div> </div> </div> </div> Clicking on submit redirects to new with an error - Body can't be blank, although I filled the body field
Logs
Started POST "/comments" for 127.0.0.1 at 2016-05-23 21:46:51 +0300 Processing by CommentsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"N92z79EAk5w3f1UwpjEiDy3x4PIPoMFVWR3LbfBJqUMGCOzzPJyPt+yzAtaFtzCuOoMcBShGKr+423nm8ZSMUw==", "comment"=>{"body"=>"test comment"}, "commit"=>"Create Comment"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] (0.1ms) begin transaction (0.1ms) commit transaction (0.1ms) begin transaction (0.1ms) rollback transaction Rendered comments/_form.html.erb (2.7ms) Rendered comments/new.html.erb within layouts/application (4.0ms) Rendered layouts/_navigation.html.erb (1.8ms) Rendered layouts/_messages.html.erb (0.1ms) Rendered layouts/_footer.html.erb (0.1ms) Completed 200 OK in 132ms (Views: 120.2ms | ActiveRecord: 0.5ms)