验证并更新单个属性轨道 [英] validate and update single attribute rails

查看:91
本文介绍了验证并更新单个属性轨道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户模式下

  attr_accessible:头像,:电子邮件

validates_ presence_of:电子邮件
has_attached_file:头像#回形针

validates_attachment_size:头像,
                          :less_than => 1.megabyte,
                          :消息=>'图片不能在大小超过1MB',
                          :如果=> Proc.new {|进口| !imports.avatar_file_name.blank? }
 

在我的控制器之一,我只想更新​​和验证的化身领域的无需更新和确认电子邮件

我怎样才能做到这一点?

例如(这是不行的)

 如果@ user.update_attributes(PARAMS [:用户])
 # 做一点事...
结束
 

我也试过用 update_attribute(阿凡达,则params [:用户] [:头像]),但会跳过验证的化身场以及<。 / P>

解决方案

您可以<一href="http://stackoverflow.com/questions/457239/is-there-a-way-to-validate-a-specific-attribute-on-an-activerecord-without-instan/457313#457313">validate用手属性和使用 update_attribute ,这跳过验证 。如果添加<一个href="http://stackoverflow.com/questions/457239/is-there-a-way-to-validate-a-specific-attribute-on-an-activerecord-without-instan/457313#457313">this你的用户

 高清self.valid_attribute?(ATTR,值)
  模拟= self.new(ATTR =&GT;值)
  如果mock.valid?
    真正
  其他
    !mock.errors.has_key?(ATTR)
  结束
结束
 

然后正是如此更新的属性:

 如果(User.valid_attribute(阿凡达,则params!?[:用户] [:头像])
    #投诉或什么的。
结束
@ user.update_attribute(阿凡达,则params [:用户] [:头像])
 

您应该让您的单一属性更新,而只(手动)验证该属性。

如果你看看米兰Novota的 valid_attribute?是如何工作的,你会看到它执行验证,然后检查是否具体 ATTR 有问题;这并不重要,如果其他任何验证的失败,因为 valid_attribute?只着眼于验证失败那些你感兴趣的属性。

如果你打算做了很多这方面的东西,那么你可以添加一个方法到User:

 高清update_just_this_one(ATTR,值)
    提高坏#{ATTR}如果(!User.valid_attribute?(ATTR,值))
    self.update_attribute(ATTR,值)
结束
 

和用它来更新您的单个属性。

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.

解决方案

You could validate the attribute by hand and use update_attribute, that skips validation. If you add this to your User:

def self.valid_attribute?(attr, value)
  mock = self.new(attr => value)
  if mock.valid?
    true
  else
    !mock.errors.has_key?(attr)
  end
end

And then update the attribute thusly:

if(!User.valid_attribute?('avatar', params[:user][:avatar])
    # Complain or whatever.
end
@user.update_attribute('avatar', params[:user][:avatar])

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 specific attr had issues; it doesn't matter if any of the other validations failed as valid_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:

def update_just_this_one(attr, value)
    raise "Bad #{attr}" if(!User.valid_attribute?(attr, value))
    self.update_attribute(attr, value)
end

and use that to update your single attribute.

这篇关于验证并更新单个属性轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆