Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Why are the rails errors not showing up?

I have this form_for

<%= form_for([@post, @post.comments.build]) do |f| %>

<% if @post.errors.any? %>
   <div id="error_explanation">
     <ul>
     <% @post.errors.full_messages.each do |msg| %>
       <li><%= msg %></li>
     <% end %>
     </ul>
   </div>
<% end %>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>

Upvotes: 1

Views: 92

Answers (1)

Peter Brown
Peter Brown

Reputation: 51707

You are not seeing errors because you are creating a new object for the form every time it is loaded:

@post.comments.build

You should add an instance variable to your controller and use that in the form instead:

@comment = @post.comments.build

<%= form_for([@post, @comment]) do |f| %>

Upvotes: 3

Related Questions