Mar*_*lar 6 ruby-on-rails ruby-on-rails-4
:restrict_with_error如果存在关联的对象rails关联基础,则会将错误添加到所有者
我在代码中添加了以下内容:
class Owner < ActiveRecord::Base
has_many :things, dependent: :restrict_with_error
end
Run Code Online (Sandbox Code Playgroud)
我的理解是,当我试图删除的业主有相关的东西,然后错误应该提高.在我的show动作中,owners_controller我尝试访问错误但无法找到它们:
def show
@owner = Owner.find(params[:id])
@owner.errors
end
Run Code Online (Sandbox Code Playgroud)
更新 - 删除代码
def destroy
@owner = Owner.find(params[:id])
@owner.destroy
flash[:notice] = "Owner Deleted Successfully"
respond_with(@owner)
end
Run Code Online (Sandbox Code Playgroud)
鉴于你的代码......
def destroy
@owner = Owner.find(params[:id])
@owner.destroy
flash[:notice] = "Owner Deleted Successfully"
respond_with(@owner)
end
def show
@owner = Owner.find(params[:id])
@owner.errors
end
Run Code Online (Sandbox Code Playgroud)
在您尝试访问错误时,将不会有任何错误.
错误是暂时的.它们不会持久存在于对象中,并且它们不会交叉请求.它们仅存在于生成错误的同一请求中的模型上.
在您调用之后,代码中唯一可以提供错误的点就在内部destroy.它们永远不会出现在您的行动中.@owner.destroyshow
def destroy
@owner = Owner.find(params[:id])
@owner.destroy
# You must check for @owner.errors here
flash[:notice] = "Owner Deleted Successfully"
respond_with(@owner)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7125 次 |
| 最近记录: |