Ruby-on-rails 在 Rails 4 中 before_action 返回 false 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20623616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:08:35  来源:igfitidea点击:

What does before_action returning false do in Rails 4?

ruby-on-railsruby-on-rails-4

提问by zer0uno

I am reading the "Agile Web Development with Rails 4", on pag. 338 it says:

我正在阅读“使用 Rails 4 进行敏捷 Web 开发”,页码。338 它说:

[...] Callbacks can be passive, monitoring activity performed by a controller. They can also take a more active part in request handling. If a before action callback returns false, then processing of the callback chain terminates, and the action is not run. [...]

[...] 回调可以是被动的,监视由控制器执行的活动。他们还可以更积极地参与请求处理。如果操作前回调返回 false,则回调链的处理终止,并且不会运行该操作。[...]

Now my doubt is the following: here how to execute an action if the before_action returns falseit was told that the goal of the before_action is to prepare something before the action is executed, if it returns false it doesn't mean that the action is not run, but according the book it is right so...so I am getting a little bit confused.

现在我的疑问如下:这里如果 before_action 返回 false如何执行一个动作被告知 before_action 的目标是在执行动作之前准备一些东西,如果它返回 false 并不意味着该动作是没有运行,但根据书中的说法是正确的,所以......所以我有点困惑。

If I'm trying the following

如果我正在尝试以下

class ProductsController < ApplicationController
before_action :test

  def index
    @products = Product.all
  end


  private

    def test
      return false
    end
end

But the action is executed, when I call /productsI do not get any error and the page shows up just fine

但是操作已执行,当我打电话时/products我没有收到任何错误并且页面显示得很好

回答by Simone Carletti

before_action(formerly named before_filter) is a callback that is performed before an action is executed. You can read more about controller before/after_action.

before_action(以前称为before_filter)是在执行操作之前执行的回调。您可以阅读有关控制器 before/after_action 的更多信息。

It is normally used to prepare the action or alter the execution.

它通常用于准备动作或改变执行。

The convention is that if any of the methods in the chain render or redirect, then the execution is halted and the action is not rendered.

约定是,如果链中的任何方法呈现或重定向,则执行将停止并且不呈现动作。

before_action :check_permission

def hello
end

protected

def check_permission
  unless current_user.admin?
    # head is equivalent to a rendering
    head(403)
  end
end

In this example, if current_user.admin?returns false, the helloaction is never performed.

在此示例中,如果current_user.admin?返回 false,hello则永远不会执行该操作。

The following one, instead, is an example of action setup:

相反,以下是操作设置的示例:

before_action :find_post

def show
  # ...
end

def edit
  # ...
end

def update
  # ...
end

protected

def find_post
  @post = Post.find(params[:id])
end

In this case find_postwill never return false. In fact, the purpose of this before_action is to extract a shared command from the body of the actions.

在这种情况下find_post永远不会返回false。事实上,这个 before_action 的目的是从动作的主体中提取一个共享命令。

About returning false, as far as I know this is true for ActiveRecord callbacks. But for a before_action, returning false has no effect. In fact, the return value is not mentioned as important in the official documentation.

关于返回false,据我所知,这对于 ActiveRecord 回调是正确的。但是对于 before_action,返回 false 没有任何效果。事实上,官方文档中并没有提到返回值那么重要。