around_action 回调如何工作?需要一个解释
How does an around_action callback work? An explanation is needed
我对around_action感到非常困惑。它们是如何工作的?有人可以向我提供他们如何工作的 example/explanation 吗?
这是我的 Agile Web Development 4 书中的一句话:
Around callbacks wrap the execution of actions. You can write an
around callback in two different styles. In the first, the callback is
a single chunk of code. That code is called before the action is executed. If the callback code invokes yield, the action is executed. When the action completes, the callback code continues executing. Thus, the code before the yield is like a before action callback and the code after the yield is the after action callback. If the callback code never invokes yield. the action is not run-this is the same as having a before action callback return false.
当我读到这篇文章时,我有点明白了。这是 Rails 指南
中的示例
class ChangesController < ApplicationController
around_action :wrap_in_transaction, only: :show
private
def wrap_in_transaction
ActiveRecord::Base.transaction do
begin
yield
ensure
raise ActiveRecord::Rollback
end
end
end
end
那么这里发生了什么? ActiveRecord::Base.transaction 是否从 "before" 部分开始,然后将 ActiveRecord::Rollback 作为 "after" 部分?这种方法产生了什么?是表演吗?最后什么会导致 yield 方法失败导致整个回调失败呢?会不会是表演动作的渲染?我不明白。请帮忙
我的理解如下:
begin
# Do before action...
logger.info 'I am the before action'
# Do the action, which is passed as a block to your "around filter"
# Note that if you were to delete this line, the action will never be called!
yield
# Do after action...
logger.info 'I am the after action'
ensure
raise ActiveRecord::Rollback
end
around_callback 的关键字是 yield
。在 wrap_in_transaction
示例的情况下:yield 被替换为 show
操作。当显示结束(包括渲染)时,wrap_in_transaction 继续并执行回滚。
在railsguides你可以找到:
For example, in a website where changes have an approval workflow an administrator could be able to preview them easily, just apply them within a transaction: ... Note that an around filter wraps also rendering. In particular, if in the example above the view itself reads from the database via a scope or whatever, it will do so within the transaction and thus present the data to preview."
这意味着 show
的用户可以在回滚之前看到信息(在这种情况下 show
必须进行某种需要回滚的更新,因为它是一个信息操作) .
你可以认为一个around_callback是前回调和后回调只有一个方法,使用yield
把动作放在中间。
我对around_action感到非常困惑。它们是如何工作的?有人可以向我提供他们如何工作的 example/explanation 吗?
这是我的 Agile Web Development 4 书中的一句话:
Around callbacks wrap the execution of actions. You can write an around callback in two different styles. In the first, the callback is a single chunk of code. That code is called before the action is executed. If the callback code invokes yield, the action is executed. When the action completes, the callback code continues executing. Thus, the code before the yield is like a before action callback and the code after the yield is the after action callback. If the callback code never invokes yield. the action is not run-this is the same as having a before action callback return false.
当我读到这篇文章时,我有点明白了。这是 Rails 指南
中的示例class ChangesController < ApplicationController
around_action :wrap_in_transaction, only: :show
private
def wrap_in_transaction
ActiveRecord::Base.transaction do
begin
yield
ensure
raise ActiveRecord::Rollback
end
end
end
end
那么这里发生了什么? ActiveRecord::Base.transaction 是否从 "before" 部分开始,然后将 ActiveRecord::Rollback 作为 "after" 部分?这种方法产生了什么?是表演吗?最后什么会导致 yield 方法失败导致整个回调失败呢?会不会是表演动作的渲染?我不明白。请帮忙
我的理解如下:
begin
# Do before action...
logger.info 'I am the before action'
# Do the action, which is passed as a block to your "around filter"
# Note that if you were to delete this line, the action will never be called!
yield
# Do after action...
logger.info 'I am the after action'
ensure
raise ActiveRecord::Rollback
end
around_callback 的关键字是 yield
。在 wrap_in_transaction
示例的情况下:yield 被替换为 show
操作。当显示结束(包括渲染)时,wrap_in_transaction 继续并执行回滚。
在railsguides你可以找到:
For example, in a website where changes have an approval workflow an administrator could be able to preview them easily, just apply them within a transaction: ... Note that an around filter wraps also rendering. In particular, if in the example above the view itself reads from the database via a scope or whatever, it will do so within the transaction and thus present the data to preview."
这意味着 show
的用户可以在回滚之前看到信息(在这种情况下 show
必须进行某种需要回滚的更新,因为它是一个信息操作) .
你可以认为一个around_callback是前回调和后回调只有一个方法,使用yield
把动作放在中间。