对于我要传递的记录的属性(如required(:title).filled),验证被触发并按预期工作,但对于嵌套模型的属性(即artist中的required(:name).filled )则不起作用。
class AlbumForm < Reform::Form
property :title
validation do
required(:title).filled
end
property :artist do
property :name
validation do
required(:name).filled
end
end
end(摘自http://trailblazer.to/gems/reform的片段)
如果是Albumform.new(album).valid?,我希望album.artist.name == nil返回false,但它不返回。我在这里错过了什么?如何才能做到这一点?
使用:
发布于 2017-07-11 20:48:14
长话短说,您应该使用validate(params[:album])而不是valid? http://trailblazer.to/gems/reform/#validation
def create
# params album: { name: nil, other_stuff: 'stuff' }
form = AlbumForm.new(Album.new)
if form.validate(params[:album])
form.save
end
endhttps://stackoverflow.com/questions/40911544
复制相似问题