如何绕过为Rails中的ruby中的嵌套属性设置的验证规则? [英] How do I bypass validation rules set for a nested attribute in ruby on rails?
问题描述
我有一个微博表格,允许用户上传照片并输入一些内容.图像文件字段是我的照片模型中的嵌套属性.
I have a micropost form which allows a user to upload a photo and type some content to go with it. The image file field is the nested attribute from my photo model.
它具有验证规则存在=> true".对于微博,这不是必需的.允许用户发布没有图片/照片的微博.
It has a validation rule "presence => true". This is not required for microposts. User are allowed to post microposts without images/photos.
我曾经为用户的图片库使用相同的照片模型,并且在提交表单时需要一张照片,所以我不能禁用此规则.
How ever I use the same photo model for the users image gallery and a photo is required at the time of form submission so I can't disable this rule.
有什么方法可以绕过我在照片模型中发布验证表单时设置的验证规则?
Is there any way to bypass the validation rule set in my photo model for when I post form the micropost form?
控制器:
def new
@user = User.new
@micropost = Micropost.new(:user_id => users_id)
@micropost.build_photo(:photo_album_id => current_user.photo_albums.find_by_album_title("microposts album").id)
end
表格:
= form_for @micropost, :html => { :multipart => true }, :remote => true do |f|
= f.fields_for :photo do |p|
= p.hidden_field :photo_album_id
= p.text_field :photo_title
= p.file_field :image, :id => "micropost_image"
= f.hidden_field :user_id
= f.text_area :content
= f.submit "Post"
Micropost模型:
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments, :dependent => :destroy
has_one :photo, :dependent => :destroy
accepts_nested_attributes_for :photo
attr_accessor :username
attr_accessible :content, :user_id, :poster_id, :username, :remote_image_url, :photo_attributes
validates :content, :presence => true, :length => { :maximum => 10000 }
validates :user_id, :presence => true
end
照片模型:
class Photo < ActiveRecord::Base
belongs_to :photo_album
attr_accessible :photo_album_id, :photo_title, :image, :remote_image_url
mount_uploader :image, ImageUploader
alpha_num_non_word_char = /^[a-zA-Z0-9\D_ -]*$/
validates :image, :presence => true
validates :photo_title, :length => { :minimum => 2, :maximum => 50 },
:format => { :with => alpha_num_non_word_char,
:message => "error"
}, :if => :photo_title?
validate :picture_size_validation, :if => "image?"
def picture_size_validation
errors[:image] << "Your photo should be less than 1MB" if image.size > 1.megabytes
end
end
亲切的问候
推荐答案
有一个选项:reject_if,您可以将其传递给accepts_nested_attributes_for,以便它在某些条件下不会尝试创建新照片.它将像这样工作:
There's an option, :reject_if, you can pass to accepts_nested_attributes_for so that it won't try to create a new photo under certain conditions. It would work like this:
accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? }
由于您将image字段的:id指定为'micropost_image',因此您可能必须在proc中像这样引用它:
Since you specified the :id of the image field as being 'micropost_image', you might have to reference it within the proc like this instead:
attributes['micropost_image']
这两个中的一个应该起作用.
One of those two should work.
这篇关于如何绕过为Rails中的ruby中的嵌套属性设置的验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!