Rails form validation not working, no output -


i trying validation working comment form assignment. reason flash error being returned me. way reacting (absolutely no output), suspect have not specified things properly.

comments need created now, generate comments_controller appropriate action;

fill in create action. should create new comment associated post , current_user created it;

flash[:error] = "error saving comment. please try again." 

_form.html.erb

<%= form_for [topic, post, comment] |f| %>   <% if comment.errors.any? %>     <div class="alert alert-danger">       <h4>there <%= pluralize(comment.errors.count, "error") %>.</h4>       <ul>         <% comment.errors.full_messages.each |msg| %>           <li><%= msg %></li>         <% end %>       </ul>     </div>     <% end %>      <div class="form-inline">     <%= form_group_tag(comment.errors[:body]) %>       <%= f.label :body %>       <%= f.text_field :body, class: 'form-control'%>     <% end %>      <div class="form-group">       <%= f.submit "comment", class: 'btn btn-default' %>     </div>   </div>   <% end %> 

in form_group_tag specified :body match label/text field.

here comment model :body , :user being specified

class comment < activerecord::base   belongs_to :post   belongs_to :user    # minimum 5 characters , must signed in   validates :body, length: { minimum: 5 }, presence: true   validates :user, presence: true end 

comments_controller.rb

class commentscontroller < applicationcontroller   def create     # find topic id     @topic = topic.find(params[:topic_id])     # find post id through topic     @post = @topic.posts.find(params[:post_id])     # comments on post     @comments = @post.comments      @comment = current_user.comments.build(params.require(:comment).permit(:body, :post_id))     @comment.post = @post      if @comment.save       flash[:notice] = "your comment created."       redirect_to [@topic, @post]     else       flash[:error] = "error saving comment. please try again."       redirect_to [@topic, @post]     end   end end 

if press submit button flash error appears, nothing related validation returned me.

in comments_controller.rb redirect on error, reloads page , resets record. should render form view.

try replacing line

redirect_to [@topic, @post] 

with

render :new 

if @comment.save fails. (i assume you're following conventional naming.)

edit:

you'll might want render view includes "comments/_form" partial. assumed "comments/new", judging comments might looking "posts/show". render defaults current controller's views, can overridden.

render "posts/show" 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -