在 Rails 4 中 before_action 返回 false 有什么作用? [英] What does before_action returning false do in Rails 4?

查看:37
本文介绍了在 Rails 4 中 before_action 返回 false 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读页面上的使用 Rails 4 进行敏捷 Web 开发".338它说:

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

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

[...] 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. [...]

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

Now my doubt is the following: here how to execute an action if the before_action returns false it 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.

如果我正在尝试以下

class ProductsController < ApplicationController
before_action :test

  def index
    @products = Product.all
  end


  private

    def test
      return false
    end
end

但是执行了操作,当我调用 /products 时,我没有收到任何错误,页面显示正常

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

推荐答案

before_action(以前命名为 before_filter)是在执行操作之前执行的回调.您可以阅读有关 controller before/after_action 的更多信息.

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.

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

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

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

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

以下是操作设置的示例:

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

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

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

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

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.

这篇关于在 Rails 4 中 before_action 返回 false 有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆