0

我想让所有错误出现在各自区域的顶部。

我在第一个上运行了一个if声明,any?但我知道我在重复自己并且必须按照 Rails 的方式来做。

有什么帮助吗?

<%= form_for @movie, :html => {:class => "form-horizontal"} do |f| %>

      <div class="control-group">
      <% if @movie.errors[:title].any? %>
        <div class="alert alert-error"><%= @movie.errors[:title].to_sentence %></div>
      <% end %>

         <%= f.label :title, :class => "control-label" %> 
           <div class="controls">
              <%= f.text_field :title %>
           </div>
      </div>
      <div class="control-group">
        <div class="alert alert-error"><%= @movie.errors[:description].to_sentence %></div>
         <%= f.label :description,:class => "control-label" %> 
            <div class="controls">
             <%= f.text_area :description, :class => "span8", :rows => "10" %>
            </div>
      </div>
      <div class="control-group">
        <div class="alert alert-error"><%= @movie.errors[:rating].to_sentence %></div>
        <%= f.label :rating, :class => "control-label" %> 
          <div class="controls">
            <%= f.select :rating, Movie::RATINGS, prompt: "Pick one" %>
          </div>
      </div>
      <div class="control-group">
        <div class="alert alert-error"><%= @movie.errors[:total_gross].to_sentence %></div>
        <%= f.label :total_gross, :class => "control-label" %>
          <div class="controls">
            <%= f.number_field :total_gross %>
          </div>
      </div>
      <div class="control-group">
        <div class="alert alert-error"><%= @movie.errors[:released_on].to_sentence %></div>
        <%= f.label :released_on, :class => "control-label" %>
          <div class="controls">
         <%= f.date_select :released_on, :order => [:month, :day, :year], :prompt => { :month => 'Select month',:day => 'Select day', :year => 'Select year' }, :start_year => 1950  %>
          </div>
      </div>
      <div class="control-group" >
        <%= f.label :image_file_name, :class => "control-label" %>
          <div class="controls">
            <%= f.text_field :image_file_name %>
          </div>
      </div>
      <div class="form-actions">
        <%= f.submit :class => "btn btn-success btn-large" %>  <%= link_to "Cancel", root_path, class: "btn btn-danger btn-large" %>
      </div>


    <% end %>
4

2 回答 2

1

这是一个更干燥的解决方案,不需要像 simple_form 这样的宝石。

为您的控制组创建一个部分,并将字段名称和表单字段帮助器替换为变量:

# File: _control_group.html.erb
<% show_errors = true if show_errors.nil? %>

<div class="control-group">
  <% if @movie.errors[field_name].present? && show_errors %>
    <div class="alert alert-error">
      <%= @movie.errors[field_name].to_sentence %>
    </div>
  <% end %>

  <%= label_tag field_name, :class => "control-label" %>

  <div class="controls">
    field_helper
  </div>
</div>

然后用部分渲染替换表单中的这些项目,并通过参数传递适当的代码:

<%= form_for @movie, :html => {:class => "form-horizontal"} do |f| %>
  <%= render "control_group", field_name: :title,           field_helper: f.text_field(:title) %>
  <%= render "control_group", field_name: :description,     field_helper: f.text_area(:description, :class => "span8", :rows => "10") %>
  <%= render "control_group", field_name: :rating,          field_helper: f.select(:rating, Movie::RATINGS, prompt: "Pick one") %>
  <%= render "control_group", field_name: :total_gross,     field_helper: f.number_field(:total_gross) %>
  <%= render "control_group", field_name: :released_on,     field_helper: f.date_select(:released_on, :order => [:month, :day, :year], :prompt => { :month => 'Select month',:day => 'Select day', :year => 'Select year' }, :start_year => 1950) %>
  <%= render "control_group", field_name: :image_file_name, field_helper: f.text_field(:image_file_name), show_errors: false %>

  <div class="form-actions">
    <%= f.submit :class => "btn btn-success btn-large" %>  <%= link_to "Cancel", root_path, class: "btn btn-danger btn-large" %>
  </div>
<% end %>
于 2013-05-11T07:39:19.393 回答
0

遇到了类似的问题。

使用simple_form对我有用。在此处查看示例

于 2013-05-11T05:29:48.303 回答