2013-05-10 70 views
0

我想讓所有的錯誤都出現在它們各自的區域之上。如何修復這種形式的Rails方式?

我跑了一個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 %> 

回答

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 %> 
+0

非常感謝你,每個人都喜歡用簡單的形式使用簡單的形式! – gustavoanalytics 2013-05-12 15:34:53

0

出現類似的問題。

使用simple_form爲我工作。看一個例子here