嗨,我有嵌套资源:
resources :posts do
resources :msgs
end
和一些验证:
class Msg < ActiveRecord::Base
belongs_to :post
validates :body ,presence:true
end
控制器:
# msgs_controller.erb
def create
@post = Post.find(params[:post_id])
@msg=@post.msgs.build(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
和一个视图:编辑:/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 %>
当正文字段为空白时,该应用程序不会显示错误消息,但验证没问题,因为服务器向我显示 ROLLBACK 而不是 COMMIT。问题是在视图中显示错误消息,你能帮我吗?