简体   繁体   English

around_action回调如何工作? 需要解释

[英]How does an around_action callback work? An explanation is needed

I am terribly confused about an around_action. 我对around_action非常困惑。 How do they work? 他们是如何工作的? Can someone please provide me with an example/explanation of how they work? 有人可以请我提供他们如何工作的例子/解释吗?

This is a quote from my Agile Web Development 4 book: 这是我的敏捷Web开发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. 如果回调代码调用yield,则执行该操作。 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. 因此,yield之前的代码就像before action回调,yield之后的代码就是after action回调。 If the callback code never invokes yield. 如果回调代码从不调用yield。 the action is not run-this is the same as having a before action callback return false. 该操作未运行 - 这与之前的操作回调返回false相同。

I sort of get it when I read this. 当我读到这篇文章时,我会得到它。 Here is an example from the Rails Guides 以下是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

So what is happening here? 那么这里发生了什么? Does ActiveRecord::Base.transaction begin as the "before" part and raise ActiveRecord::Rollback as the "after" part? ActiveRecord :: Base.transaction是否作为“before”部分开始,并将ActiveRecord :: Rollback作为“after”部分引发? What is this method yielding to? 这种方法屈服于什么? Is it the show? 是节目吗? Finally what would cause the yield method to fail causing the entire callback to fail? 什么会导致yield方法失败导致整个回调失败? Would it be the rendering of the show action? 这是展示动作的渲染吗? I don't get it. 我不明白。 Help please. 请帮忙。

My understanding is as below: 我的理解如下:

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

The key of the around_callback is yield . around_callback的关键是yield In the case of the wrap_in_transaction example: yield is replaced with the show action. 对于wrap_in_transaction示例:yield将替换为show动作。 When show ends (rendering inclusive), wrap_in_transaction continues and performs the rollback. 当show结束(呈现包含)时,wrap_in_transaction继续并执行回滚。

At rails guides you can find: 在导轨指南,您可以找到:

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." 特别是,如果在上面的示例中,视图本身通过作用域或其他内容从数据库中读取,它将在事务中执行此操作,从而将数据呈现为预览。

That means the user at show can see the information before the rollback (in this case show must be doing a sort-of update which need a rollback because it is an information action). 这意味着show的用户可以在回滚之前看到信息(在这种情况下show必须进行一种需要回滚的排序更新,因为它是一个信息操作)。

You can think that an around_callback is a before callback and an after callback in only one method, using yield to put the action in the middle. 你可以认为around_callback只是一个方法之前的回调和后回调,使用yield将动作放在中间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM