Alright, I’ve got to be missing something simple.
I’m using a partial “/shared/_error_messages.html.erb” to handle
<%= render 'shared/error_messages', object: f.object %>
in my forms (one for adding programs, one for adding metrics).
When I navigate to any form (/programs/new and metrics/new), the validation appears when the page loads.
The programs_controller and metrics_controller are structurally the same (swapping @metrics for @programs in metrics_controller):
#programs_controller.rb
def new
@programs = Program.new(params[:name])
if @programs.save
flash[:success] = "Program saved"
redirect_to "/program"
else
render 'new'
end
end
any ideas what might be causing this?
Here’s the partial:
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
I don’t know what the errors are but the problem is that your are saving your object in the
newaction. The saving generates the object errors; that is why you see them.In a
RESTfulway, the new action should just instantiate a model and pass the object to your form. The form will submit it to thecreateaction; where you should save your object and check errors.Something like: