rails fields_for does not render after validation error on nested form

后端 未结 3 502
栀梦
栀梦 2020-12-16 22:27

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 pe

相关标签:
3条回答
  • 2020-12-16 23:08

    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 讨论(0)
  • 2020-12-16 23:12

    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.

    0 讨论(0)
  • 2020-12-16 23:19

    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.

    0 讨论(0)
提交回复
热议问题