Ruby on rails 所有字段的验证都会失败
我在轨道3上。我有一个名为Ruby on rails 所有字段的验证都会失败,ruby-on-rails,forms,ruby-on-rails-3,Ruby On Rails,Forms,Ruby On Rails 3,我在轨道3上。我有一个名为Client的模型,它有name、phone和email。我的模型文件如下所示: class Client < ActiveRecord::Base belongs_to :salon belongs_to :address validates_presence_of :name validates_presence_of :phone validates_presence_of :email accepts_nested_attribut
Client
的模型,它有name
、phone
和email
。我的模型文件如下所示:
class Client < ActiveRecord::Base
belongs_to :salon
belongs_to :address
validates_presence_of :name
validates_presence_of :phone
validates_presence_of :email
accepts_nested_attributes_for :address
attr_accessible :address_attributes
end
下面是我的创建操作:
def create
@client = Client.new(params[:client])
respond_to do |format|
if @client.save
format.html { redirect_to(@client, :notice => 'Client was successfully created.') }
format.xml { render :xml => @client, :status => :created, :location => @client }
else
format.html { render :action => "new" }
format.xml { render :xml => @client.errors, :status => :unprocessable_entity }
end
end
end
知道为什么会发生这种情况吗?这是因为您将:address\u attributes
设置为唯一可访问的属性。改变
attr_accessible :address_attributes
到
或者不要使用批量分配。您可以包括创建操作吗。此外,表单的嵌套部分是否正在输出?是否编辑为包含创建操作。不,嵌套部分没有输出,我不知道为什么。我很想这样做,但我决定暂时不解决这个单独的问题。您需要包含一个address实例或构建一个内联实例:或者让字段显示出来。谢谢,我试试看。你说的质量分配到底是什么意思?顺便说一句,这解决了问题。谢谢。@Jason Swett检查一下这个
attr_accessible :address_attributes
attr_accessible :address_attributes, :name, :phone, :email