views:

13

answers:

1

I have a one to many relationship with a child form that is nested using a fields_for operation.

I want to use the built in rails error handling to show error messages on the children using the error_message_on method.

Example:

      <% 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>
...
+1  A: 

Did u tried f.error_messages for displaying business errors and for child hours form, use hours_form.error_messages.

example

     <% 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>
Senthil Kumar Bhaskaran