验证和更新单一属性轨
我的一个控制器的用户模型中有以下内容
attr_accessible :avatar, :email
validates_presence_of :email
has_attached_file :avatar # paperclip
validates_attachment_size :avatar,
:less_than => 1.megabyte,
:message => 'Image cannot be larger than 1MB in size',
:if => Proc.new { |imports| !imports.avatar_file_name.blank? }
,我只想更新和验证头像字段而不更新和验证电子邮件。
我该如何执行此操作?
例如(这不起作用)
if @user.update_attributes(params[:user])
# do something...
end
我还尝试使用 update_attribute('avatar', params[:user][:avatar])
,但这也会跳过 avatar 字段的验证。
I have the following in my user model
attr_accessible :avatar, :email
validates_presence_of :email
has_attached_file :avatar # paperclip
validates_attachment_size :avatar,
:less_than => 1.megabyte,
:message => 'Image cannot be larger than 1MB in size',
:if => Proc.new { |imports| !imports.avatar_file_name.blank? }
in one of my controllers, I ONLY want to update and validate the avatar field without updating and validating email.
How can I do this?
for example (this won't work)
if @user.update_attributes(params[:user])
# do something...
end
I also tried with update_attribute('avatar', params[:user][:avatar])
, but that would skip the validations for avatar field as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以手动验证属性并使用
update_attribute
,即 跳过验证。如果添加 将此信息发送给您的用户
:然后这样更新属性:
您应该在仅(手动)验证该属性的同时更新您的单个属性。
如果您查看 Milan Novota 的
valid_attribute?
的工作原理,您会发现它执行验证,然后检查特定的attr
是否存在问题;如果任何其他验证失败并不重要,因为valid_attribute?
只会查看您感兴趣的属性的验证失败。如果您打算做很多这样的事情然后你可以向 User: 添加一个方法
,并使用它来更新你的单个属性。
You could validate the attribute by hand and use
update_attribute
, that skips validation. If you add this to yourUser
:And then update the attribute thusly:
You should get your single attribute updated while only (manually) validating that attribute.
If you look at how Milan Novota's
valid_attribute?
works, you'll see that it performs the validations and then checks to see if the specificattr
had issues; it doesn't matter if any of the other validations failed asvalid_attribute?
only looks at the validation failures for the attribute that you're interested in.If you're going to be doing a lot of this stuff then you could add a method to User:
and use that to update your single attribute.
有条件吗?
A condition?
您是否尝试过在
validates_presence_of :email
上设置条件?http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ ClassMethods.html#M000083
配置选项:
Have you tried putting a condition on the
validates_presence_of :email
?http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000083
Configuration options:
我假设您需要这个,因为您有一个多步骤向导,您首先上传头像,然后填写电子邮件。
据我所知,根据您的验证,我认为没有好的可行解决方案。要么验证全部,要么更新头像而不进行验证。如果它是一个简单的属性,您可以单独检查新值是否通过验证,然后在不验证的情况下更新模型(例如使用
update_attribute
)。我可以建议两种可能的替代方法:
所以我会提出这样的建议:
希望这会有所帮助。
I am assuming you need this, because you have a multi-step wizard, where you first upload the avatar and the e-mail is filled in later.
To my knowledge, with your validations as they are, I see no good working solution. Either you validate all, or you update the avatar without validations. If it would be a simple attribute, you could check if the new value passes the validation seperately, and then update the model without validations (e.g. using
update_attribute
).I can suggest two possible alternative approaches:
So I would propose something like this:
Hope this helps.
这是我的解决方案。
它与 .valid? 方法保持相同的行为,返回 true 或 false,并在调用它的模型上添加错误。
Here is my solution.
It keeps the same behaviour than .valid? method, witch returns true or false, and add errors on the model on witch it was called.