Hi I have nested resource:
resources :posts do
resources :msgs
end
and some validations:
class Msg < ActiveRecord::Base
belongs_to :post
validates :body ,presence:true
end
a controller:
# msgs_controller.erb
def create
@post = Post.find(params[:post_id])
@[email protected](msg_params)
@msg.user=current_user
@msg.email=current_user.email
@msg.autor=current_user.name
if @msg.save
flash[:notice] = t '.create'
end
respond_with(@post,@msg)
end
and a view: EDIIT: /views/posts/show.html.rb # /views/show.html.erb
<h2>Add a comment:</h2>
<%= form_for([@post, @post.msgs.build]) do |f| %>
<% if @msg.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@msg.errors.count, "error") %> prohibited this msg from being saved:</h2>
<ul>
<% @msg.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
the app doesn't shows the errors messages when body field is blank, but the validations are ok becouse the server show me ROLLBACK instead COMMIT. The problem is to show the error messages in the views, can you help me?
@msg.errors
? What does the data in@msg
actually look like at the point when you're trying to save? You mention that it is doing a ROLLBACK, but if the validation is really failing it would not try to do an INSERT at all. See edgeguides.rubyonrails.org/… - section 1.2, second paragraph. This indicates that maybe validations are not actually failing and something else is going wrong with the save (causing the ROLLBACK). – Scott SPosts
model haveaccepts_nested_attributes_for :msg
? If so, I'm wondering if the errors would show up in@post.errors
instead. Maybe give that a try. – lurker