本文介绍了是“ proc”还是“ proc”?需要有条件的before_action / before_filter吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看,一个 before_filter

class ThingController < ApplicationController
  before_filter :check_stuff, :if => proc {Rails.env.production?}
end

在最近的代码审查中,我有人问,是否需要 proc 才能正常工作? 答案似乎是是,但这是一个合理的问题,并且我打算通过参考Rails文档或指南或使用 before_filter (现在为 before_action )。

During a recent code review, I was asked, "Is the proc is required for this to work?" The answer appears to be 'yes', but it's a reasonable question, and I had intended to answer it by referring to the Rails docs or guides or something on the use of conditionals with before_filter (now an alias of before_action).

我找不到任何东西。 提到:only / :除了,但不包括:if / :除非

I couldn't find any. The Action Controller Guide mentions :only/:except, but not :if/:unless.

失败了,我可以指出的代码中是否有涵盖此内容的地方?在进行了简短提及。有关如何处理:only :except 而不是:if :除非

Failing that, is there somewhere in the code I can point to that covers this? It's mentioned briefly here, but that's more about how :only and :except are handled, rather than :if or :unless.

推荐答案

在Rails上找到它指南:

Found it on Rails Guides: http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks

并非总是需要 Proc 才能工作。

Turns out a Proc isn't always required for it to work.

因此,在您的情况下,您可能会逃脱

So in your case you could probably get away with

before_action :check_stuff, if: "Rails.env.production?"

在Rails文档中查找内容有时会很麻烦,但是至少这样的问题使事情更容易由于StackOverflow索引编制正确且搜索排名较高,因此可以随时查找。

Finding things in Rails documentation can be a pain sometimes, but at least questions like this make things easier to find over time since StackOverflow is well indexed and has high search rankings.

这篇关于是“ proc”还是“ proc”?需要有条件的before_action / before_filter吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:32