Ruby on rails 3 如果表单验证失败,则不会显示多个的nester属性
如果表单验证失败,则具有多个关联的字段将从视图中消失: 我的新动作看起来像Ruby on rails 3 如果表单验证失败,则不会显示多个的nester属性,ruby-on-rails-3,validation,associations,Ruby On Rails 3,Validation,Associations,如果表单验证失败,则具有多个关联的字段将从视图中消失: 我的新动作看起来像 def new @realty = Realty.new @realty.build_user @realty.build_address #user, address are has_one association, and they stay in view after failed validation 6.times { @realty.realty_images.build } # and
def new
@realty = Realty.new
@realty.build_user
@realty.build_address
#user, address are has_one association, and they stay in view after failed validation
6.times { @realty.realty_images.build } # and this one vanishes away
respond_to do |format|
format.html # new.html.erb
format.json { render json: @realty }
end
end
如果我在表单中添加此代码段
- if @realty.realty_images.empty?
- 6.times { @realty.realty_images.build }
字段再次显示,但有点粗糙
我试过了
6.times { @realty.realty_images.build } if @realty.realty_images.empty?
或
在控制器中,但它不工作,验证失败时字段再次消失
创建操作:
def create
@realty = Realty.new(params[:realty])
respond_to do |format|
if @realty.save
format.html { redirect_to @realty, notice: 'Realty was successfully created.' }
format.json { render json: @realty, status: :created, location: @realty }
else
format.html { render action: "new" }
format.json { render json: @realty.errors, status: :unprocessable_entity }
end
end
end
我的表格
= simple_form_for(@realty) do |f|
= f.error_notification
.form-inputs
= f.input :offer_type
= f.input :realty_type
= f.input :rent_type
= f.input :price
= f.input :description
= f.input :state, as: :hidden
= f.input :number_of_rooms
= f.input :floor
= f.input :service, as: :hidden
= f.simple_fields_for :address do |address_f|
= address_f.input :city, :required => true
= address_f.input :state, :required => true
= address_f.input :street, :required => true
- unless user_signed_in?
= f.simple_fields_for :user do |user_f|
= user_f.input :name, :autofocus => true, :required => true
= user_f.input :phone, :required => true
= user_f.input :email, :required => true
= user_f.input :password, :required => true
= user_f.input :password_confirmation, :required => true
- if @realty.realty_images.empty?
- 6.times { @realty.realty_images.build }
= f.simple_fields_for :realty_images do |image_f|
= image_f.input :image, as: :file
.form-actions
= f.button :submit
不动产模型
class Realty < ActiveRecord::Base
attr_accessible :description, :floor, :user_id, :number_of_rooms, :price, :service, :state, :realty_images_attributes, :address_attributes, :user_attributes, :offer_type, :realty_type, :rent_type
belongs_to :user, :dependent => :destroy, :class_name => "User"
has_many :realty_images, :dependent => :destroy
has_one :address, :dependent => :destroy
validates :offer_type, :presence => true, :length => { :in => 1..256 }
validates :realty_type, :presence => true, :length => { :in => 1..256 }
validates :state, :presence => true, :length => { :in => 1..256 }
validates :description, :length => { :maximum => 2000 }
validates :service, :presence => true, :length => { :in => 1..256 }
accepts_nested_attributes_for :realty_images
accepts_nested_attributes_for :user
accepts_nested_attributes_for :address
end
class Realty:destroy,:class\u name=>“user”
拥有多个:不动产\u图像,:dependent=>:destroy
有一个:地址,:依赖=>:销毁
验证:offer_type,:presence=>true,:length=>{:in=>1..256}
验证:realty_type,:presence=>true,:length=>{:in=>1..256}
验证:state,:presence=>true,:length=>{:in=>1..256}
验证:description,:length=>{:max=>2000}
验证:服务,:presence=>true,:length=>{:in=>1..256}
接受:realty\u图像的\u嵌套\u属性\u
接受用户的\u嵌套\u属性\u
接受地址的\u嵌套\u属性\u
结束
要使失败的对象仍然存在于#new中,如果存在旧对象,可以避免创建新对象。像这样:
@realty = || Realty.new
这样#new将使用旧的失败对象而不是新对象
对于@realty对象,它将起作用。但对于用户和地址等进一步的关联,我以前没有做过类似的事情,所以您需要自己验证
p、 代码依赖于即时变量,这就是为什么我关心如何保存obj。希望这能有点帮助:)@Billy Chan谢谢你的建议,你真的帮了我。我对这个问题的解决方案有点粗糙,但效果很好 在创建操作中添加
6.times { @realty.realty_images.build } if @realty.realty_images.empty?
if@realty.save返回false
看起来像这样
def create
@realty = Realty.new(params[:realty])
@realty.user = current_user if user_signed_in?
respond_to do |format|
if @realty.save
format.html { redirect_to @realty, notice: 'Realty was successfully created.' }
format.json { render json: @realty, status: :created, location: @realty }
else
6.times { @realty.realty_images.build } if @realty.realty_images.empty?
format.html { render action: "new" }
format.json { render json: @realty.errors, status: :unprocessable_entity }
end
end
end
我认为你混淆了两个动作:新建和创建。只有当在/Realths/NeXT中访问空白窗体时才调用“新建”操作。提交表单时会调用“创建”操作 仅在“新建”操作中添加6个示例图像。在创建操作中,
Realty.new
将丢弃空白(未填充)的图像。这就是为什么你会看到他们消失。您需要在验证错误时重新添加它们。您可以将公共设置提取到一个方法中:
class RealtiesController
def new
@realty = Realty.new
@realty.populate
end
def create
@realty = Realty.new(params[:realty])
if @realty.save
# ...
else
@realty.populate
render 'new'
end
end
end
class Realty
# you could also place it in the controller
# if you feel it fits better there
def populate
6.times{ self.realty_images.build }
end
end
请展示#创建,这是关键。我把它添加到我的问题中。但这是相当标准的:)似乎不好。你没有逻辑在create中保存用户、地址和图像?我删除了这个剪掉的“@realty.user=current\u user if user\u signed\u in?”因为在这个问题中不需要它。事实上,验证失败后的地址和用户字段是存在的,如果验证通过,创建操作将创建所有必要的字段。稍后我将为此编写测试,因为它对我来说有点新,但我在rails控制台和realty.user中调试它,realty.address显示有效的用户和地址它接受\u嵌套的属性\u for:realty\u图像接受\u嵌套的属性\u for:user接受\u嵌套的属性\u for:address你是说@realty | |=realty.new?这并不能解决问题。我只有接受嵌套属性的问题:realty\u images字段,在验证失败后它们消失,所有其他字段都保留在那里。谢谢你的帮助。我已经将应用程序部署到我的VPS,您可以在这里查看@AndreyAsinishyn,我以前没有类似的案例。但我认为解决方案是使用一个image实例来构建表单,并保留此实例不变,并在创建失败时交付给#new if#create。你是什么意思?如何实现这一点?可能使用我在view“6.times{@realty.realty\u images.build}if@realty.realty\u images.empty”中的粗略方法?然后像噩梦一样忘记它?你的意思是像这样
def create…。if@realty.save…。否则6.times{@realty.realty\u images.build}如果@realty.realty\u images.empty?…end
原则上,这解决了问题。请将其添加为答案。这通常是相同的,但更优雅的方法。这是可行的,但与realty\u images表单字段相关的验证错误不会显示:(是的,它们与主模型(realty)是分开的。您必须获取并显示每个模型的realty_image.errors。
class RealtiesController
def new
@realty = Realty.new
@realty.populate
end
def create
@realty = Realty.new(params[:realty])
if @realty.save
# ...
else
@realty.populate
render 'new'
end
end
end
class Realty
# you could also place it in the controller
# if you feel it fits better there
def populate
6.times{ self.realty_images.build }
end
end