2010-08-30 27 views
0

我與使用fields_for操作嵌套的子表單有一對多的關係。如何設置一個field_for子表單的字段的error_message_on

我想使用內置的rails錯誤處理在使用error_message_on方法的孩子上顯示錯誤消息。

例子:

 <% form_for @business, :url => {:action => :page, :page => @page}, :html => {:method => :put } do |f| -%> 
     <h5><strong>More Details</strong></h5> 
     <div class="clear"></div> 
     <div class="col-1"> 
      <label for="hours">Hours</label> 
      <table class="hours"> 
      <tbody> 
       <% f.fields_for :hours do |hours_form| %> 
       <tr> 
       <td><%= hours_form.label :day, hours_form.object.day %>:<%= hours_form.hidden_field :day %></td> 
       <td><%= hours_form.text_field :open_time, :class => 'input' %></td> 
       <td>to</td> 
       <td><%= hours_form.text_field :close_time, :class => 'input' %></td> 
       <td><%= hours_form.check_box :closed %></td> 
       <td class="c6"><%= hours_form.label :closed, 'Closed this day' %> 
       <%= hours_form.error_message_on :open_time, :css_class => 'cant-be-blank' %> 
       <%= hours_form.error_message_on :close_time, :css_class => 'cant-be-blank' %></td> 
       </tr> 
       <% end -%> 
      </tbody> 
      </table> 
... 

回答

2

難道ü嘗試f.error_messages顯示業務差錯和兒童小時的形式,使用hours_form.error_messages

例如

 <% form_for @business, :url => {:action => :page, :page => @page}, :html => {:method => :put } do |f| -%> 
     <%= f.error_messages %> 
     <h5><strong>More Details</strong></h5> 
     <div class="clear"></div> 
     <div class="col-1"> 
      <label for="hours">Hours</label> 
      <table class="hours"> 
      <tbody> 
       <% f.fields_for :hours do |hours_form| %> 
       <%= hours_form.error_messages %> 
       <tr> 
       <td><%= hours_form.label :day, hours_form.object.day %>:<%= hours_form.hidden_field :day %></td> 
       <td><%= hours_form.text_field :open_time, :class => 'input' %></td> 
       <td>to</td> 
       <td><%= hours_form.text_field :close_time, :class => 'input' %></td> 
       <td><%= hours_form.check_box :closed %></td> 
       <td class="c6"><%= hours_form.label :closed, 'Closed this day' %> 
       <%= hours_form.error_message_on :open_time, :css_class => 'cant-be-blank' %> 
       <%= hours_form.error_message_on :close_time, :css_class => 'cant-be-blank' %></td> 
       </tr> 
       <% end -%> 
      </tbody> 
      </table> 
相關問題