Use the helper method:
def errors_for(model, attribute)
if model.errors[attribute].present?
content_tag :span, :class => 'error_explanation' do
model.errors[attribute].join(", ")
end
end
end
And in sight:
<%= lesson_form.text_field :description %><br />
<%= errors_for @lesson, :description %>
<%= lesson_form.label :category %>
<%= lesson_form.text_field :category %><br />
<%= errors_for @lesson, :category %>
<% end %>
Or you can use simple_form , which will do all this for you as follows:
<%= simple_form_for @lesson do |f| %>
<%= f.input :description %>
<%= f.input :category %>
<%= f.button :submit %>
<% end %>
And if you use simple_form and haml , everything looks a bit neat:
= simple_form_for @lesson do |f|
= f.input :description
= f.input :category
= f.button :submit
(, , , ..), f.input.