我正在使用Bootstrap-Sass以及Formstatic。我想到了一个错误信息会自动显示在这张照片旁边的Formstatic领域,如:here http://asciicasts.com/system/photos/227/original/E185I06.pngRails:表单验证错误未显示(使用Formtastic和Bootstrap-Sass)
但是,即使用户将输入无效,我的应用程序不显示错误消息。这似乎是一个简单的问题,但我无法弄清楚背后的原因。
PostController中
# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])
@post.view = 0
@post.like = 0
@post.hate = 0
respond_to do |format|
if @post.save
@posts = Post.paginate(:page => params[:page], order: 'like desc', per_page: 10)
format.html { redirect_to posts_path }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
PostModel
validates :name, :presence => true
validates :content, :presence => true,
:length => { :minimum => 10, :maximum => 300}
_form(POST)
<% @post = Post.new %>
<%= semantic_form_for @post do |f| %>
<%= f.semantic_errors :name %>
<%= f.inputs do %>
<%= f.input :name, :label => 'name' %>
<%= f.input :content, :label => 'body' %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :button_html => { :class => "btn btn-primary" }, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
在PostController中,我试图删除以下两行
#format.html { render action: "new" }
#format.json { render json: @post.errors, status: :unprocessable_entity }
,并添加
render @post.errors
然后,我得到了
@messages={:name=>["can't be blank"], :content=>["can't be blank", "is too short (minimum is 10 characters)"]}>
所以我觉得这个问题可能是我的方式m渲染json是错误的。有人可以帮我解决这个问题吗?