开发者

rails fields_for does not render after validation error on nested form

开发者 https://www.devze.com 2023-01-13 17:39 出处:网络
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.

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


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.


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.


at this moment, the only way i find to fix that it was only overwriting the create method.

 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
      @property.images.build if @property.images.blank? ##because i'm shure you have something similar to : accepts_nested_attributes_for :images,      :reject_if => lambda { |fields| fields[:image].blank? }
      render :action => 'new'
    end
  end

hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号