rails fields_for 在嵌套表单上验证错误后不呈现 [英] rails fields_for does not render after validation error on nested form

查看:26
本文介绍了rails fields_for 在嵌套表单上验证错误后不呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套表单问题.我实现了 railscasts 196 & 的嵌套表单解决方案.197. 如果我没有验证错误,它就可以工作.

因此,表单在加载时可以完美呈现,包括嵌套字段(在 fields_for 部分).

但是,表单有验证.当验证失败时,控制器会渲染 :new.然后表单可以正常呈现链接的模型字段,但不再呈现嵌套字段.有解决办法吗?

控制器

 def new@property = Property.new@property.images.build结尾定义创建@property = Property.new(params[:property])如果@property.saveflash[:success] = t('Your_property') + ' ' + t('is_successfully_created')redirect_to myimmonatie_url别的渲染:动作=>'新的'结尾结尾

部分视图:

<% form_for :property, @property, :url =>{:动作=>"创建" }, :html =>{ :multipart =>真 } 做 |f|%><div id="new-property-form-spannedcols"><div class="formField inptRequired"><%= f.label :postal_code, t("Postal_code") %><%= f.text_field :postal_code, :class =>'inpt中短'%>

<div id="city_row" class="formField inptRequired"><%= f.label :city, t("City") %><div id="city_cell"><%=渲染:部分=>'ajax/城市', :locals =>{ :postal_code =>@property.postal_code } %>

...<% f.fields_for :images do |builder|%><div class="formField"><%= builder.label :photo, t("Photo_path_max_3mb") %><%= builder.file_field :photo, :class =>'输入中'%><%= builder.hidden_​​field :order, :value =>1"%>

<%结束%>

<!--/new-property-form-spannedcols --><div class="formBtn"><%= f.submit t("Save"), :class =>'btnMedium bg-img-home' %>&nbsp;

<!--/formBtn --><%-结束-%>

解决方案

是否抛出错误?

我的猜测是您的问题是在您的 new 操作中,您正在执行 @property.images.build,这不在您的编辑操作中.当验证失败时,它将呈现您的新操作,但不会运行您的新操作.您可以尝试将 @property.images.build 放在 create 操作的 else 子句中,例如:

else@property.images.build渲染:动作=>'新的'结尾

无论如何,这都不是最干净的方法,但这将有助于追踪是否是您的问题.

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.

这篇关于rails fields_for 在嵌套表单上验证错误后不呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆