我如何修复这种形式的Rails方式?

时间:2013-05-10 23:03:04

标签: ruby-on-rails ruby

我希望将所有错误显示在各自的区域之上。

我在第一个上用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 %>

2 个答案:

答案 0 :(得分: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 %>

答案 1 :(得分:0)

陷入类似的问题。

使用simple_form为我工作。查看示例here