我正在学习RoR并尝试使用accepts_nested_attributes_for和has_and_belongs_to_many来提交传统上是两种形式的信息.我在一些网站上看到它们兼容,有些网站不兼容,有些网站不知道.作为参考,我使用的是Rails 2.3.4.我尝试使用嵌套模型上的Ryan's Scraps教程对我的解决方案进行建模
从我试图调试,似乎我有两个问题,但我不知道为什么.
这是我的插入尝试的一段代码和相应的日志:
律师模型:
class Attorney < ActiveRecord::Base
has_and_belongs_to_many :associations
accepts_nested_attributes_for :associations, :reject_if => proc { |a| a['name'].blank? }
end
Run Code Online (Sandbox Code Playgroud)
协会模型:
class Association < ActiveRecord::Base
has_and_belongs_to_many :attorneys
accepts_nested_attributes_for :attorneys
validates_presence_of :name, :message => "Please enter an association name."
end
Run Code Online (Sandbox Code Playgroud)
律师控制员:
def new
@attorney = Attorney.new
@attorney.associations.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @attorney }
end
end
def create
@attorney = Attorney.new(params[:attorney])
respond_to do |format|
if @attorney.save
flash[:notice] = …Run Code Online (Sandbox Code Playgroud) 我在嵌套模型上关注了Ryan Bates 教程.我的几个嵌套模型都有与之关联的日期.在我的迁移中,它们实际上是"日期"类型.
我尝试过的一些事情和我遇到的问题
任何想法或提示都会有所帮助.
编辑:before_save似乎更有前途但由于某种原因,值在保存之前进入nil但在日志转储中可见.
编辑2:有趣的是,这似乎只是'更新'上的问题,而不是'创建'.