Mis*_*hko 10 ruby-on-rails update-attributes ruby-on-rails-3
要检查是否buyer.save
会失败,我使用buyer.valid?
:
def create
@buyer = Buyer.new(params[:buyer])
if @buyer.valid?
my_update_database_method
@buyer.save
else
...
end
end
Run Code Online (Sandbox Code Playgroud)
我怎么能检查是否update_attributes
会失败?
def update
@buyer = Buyer.find(params[:id])
if <what should be here?>
my_update_database_method
@buyer.update_attributes(params[:buyer])
else
...
end
end
Run Code Online (Sandbox Code Playgroud)
Ena*_*ane 14
如果没有完成则返回false,与之相同save
.save!
如果你更喜欢那样会抛出异常.我不确定是否有update_attributes!
,但这是合乎逻辑的.
做就是了
if @foo.update_attributes(params)
# life is good
else
# something is wrong
end
Run Code Online (Sandbox Code Playgroud)
http://apidock.com/rails/ActiveRecord/Base/update_attributes
那么你想要这个方法你必须写.如果你想预先检查params卫生.
def params_are_sanitary?
# return true if and only if all our checks are met
# else return false
end
Run Code Online (Sandbox Code Playgroud)
或者,取决于您的约束
if Foo.new(params).valid? # Only works on Creates, not Updates
@foo.update_attributes(params)
else
# it won't be valid.
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11342 次 |
最近记录: |