如何替换:if和:除非rails 5.2的选项

时间:2017-10-12 22:20:16

标签: ruby-on-rails

在application_controller.rb中:4

class ApplicationController < ActionController::Base
  before_action :prepare_meta_tags, if: "request.get?"

Rails 5.1警告

DEPRECATION WARNING: Passing string to be evaluated in :if and :unless conditional options is deprecated and will be removed in Rails 5.2 without replacement. Pass a symbol for an instance method, or a lambda, proc or block, instead. (called from <class:ApplicationController> at MYSITE/app/controllers/application_controller.rb:4)

问题:

  1. 什么是选项:if和:除非在该条款中重写
  2. 如何重写以符合rails 5.2
  3. 我有if,除非在我的项目中全部完成。需要建议。感谢

1 个答案:

答案 0 :(得分:27)

首先,不要担心他们不会弃用:if和:除非。他们不赞成将字符串传递给它。

在这种情况下,使用lambda会更好

class ApplicationController < ActionController::Base
  before_action :prepare_meta_tags, if: -> { request.get? }
...
end