Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails fields_for does not render after validation error on nested form

I have a nested form problem. I implemented the nested forms solution form the railscasts 196 & 197. It works if I have no validation errors.

So, form renders perfectly when it is loaded, including the nested fields (in the fields_for part).

But, the form has validations. When a validation fails, the controller does render :new. Then the form renders the linked model fields ok, but the nested fields are not anymore rendered. Is there a solution for this?

the controller

  def new
    @property = Property.new
    @property.images.build
  end

  def create
    @property = Property.new(params[:property])
    if @property.save
      flash[:success] = t('Your_property') + ' ' + t('is_successfully_created')
      redirect_to myimmonatie_url
    else
      render :action => 'new'
    end
  end

part of the view:

<% form_for :property, @property, :url => { :action => "create" }, :html => { :multipart => true } do |f| %>
  <div id="new-property-form-spannedcols">
      <div class="formField inptRequired">
        <%= f.label :postal_code, t("Postal_code") %>
        <%= f.text_field :postal_code, :class => 'inptMedium short' %>
      </div>
      <div id="city_row" class="formField inptRequired">
        <%= f.label :city, t("City") %>
        <div id="city_cell">
          <%= render :partial => 'ajax/cities', :locals => { :postal_code => @property.postal_code } %>
        </div>
      </div>

      ...

      <% f.fields_for :images do |builder| %>
        <div class="formField">
          <%= builder.label :photo, t("Photo_path_max_3mb") %>
          <%= builder.file_field :photo, :class => 'inptMedium' %>
          <%= builder.hidden_field :order, :value => "1" %>
        </div>
      <% end %>
  </div> <!-- /new-property-form-spannedcols -->
  <div class="formBtn">
    <%= f.submit t("Save"), :class => 'btnMedium bg-img-home' %>&nbsp;
  </div> <!-- /formBtn -->
<%- end -%>
like image 432
Michael Torfs Avatar asked Aug 31 '10 17:08

Michael Torfs


2 Answers

Does it throw an error?

My guess is your problem is that in your new action, you are doing @property.images.build, which is not in your edit action. When the validation fails, it will render your new action, but won't run your new action. You could try putting @property.images.build in the else clause of your create action like:

else
  @property.images.build
  render :action => 'new'
end

Not the cleanest way to do it, by any means, but this will help track down if that is your issue.

like image 149
theIV Avatar answered Oct 08 '22 06:10

theIV


I was also having the same problem with this behavior. Since I can't see your model, my guess is that you have :reject_if => :all_blank or some other lambda. This seems to be the culprit, although I don't have a fix. I would leave this as a comment instead of answer, but apparently I don't have enough reputation to do such a thing.

like image 43
awilkening Avatar answered Oct 08 '22 07:10

awilkening