Simple_form_for - 显示每个字段的验证错误
Simple_form_for - show validation errors for each field
我有一个简单的表单,从某种意义上说它运行良好,它显示了验证错误:
<%= simple_form_for( @my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<p>
<%= f.text_field :title, :placeholder => t("title") %>
</p>
......
但它只是没有显示哪些字段无效。我怎样才能做到这一点?
你应该这样使用它:
<%= simple_form_for( @my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<p>
<%= f.input :title, :placeholder => t("title") %>
</p>
......
使用 input
而不是 text_field
。
检查文档中的示例 here。
希望对您有所帮助!
<%= simple_form_for( @my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs <% unless @my_model.errors[:title].empty? %>error<% end %>">
<p>
<%= f.input_field title, :placeholder => t("title") %>
<%= f.full_error :title %>
</p>
......
我有一个简单的表单,从某种意义上说它运行良好,它显示了验证错误:
<%= simple_form_for( @my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<p>
<%= f.text_field :title, :placeholder => t("title") %>
</p>
......
但它只是没有显示哪些字段无效。我怎样才能做到这一点?
你应该这样使用它:
<%= simple_form_for( @my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<p>
<%= f.input :title, :placeholder => t("title") %>
</p>
......
使用 input
而不是 text_field
。
检查文档中的示例 here。
希望对您有所帮助!
<%= simple_form_for( @my_model, :defaults => { :input_html => { :class => "input-xlarge" } }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs <% unless @my_model.errors[:title].empty? %>error<% end %>">
<p>
<%= f.input_field title, :placeholder => t("title") %>
<%= f.full_error :title %>
</p>
......