Rails:如何替换 Rails 5.2 的 :if 和 :unless 选项 [英] Rails: How to replace :if and :unless option for rails 5.2

查看:43
本文介绍了Rails:如何替换 Rails 5.2 的 :if 和 :unless 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 application_controller.rb:4 中

in application_controller.rb:4

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

Rails 5.1 警告

Rails 5.1 WARNING

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 和 :unless 子句中的选项是什么
  2. 以及如何重写以符合 rails 5.2

我的项目中有如果和除非.需要建议.谢谢

I have if and unless all over in my project. Need advice. Thanks

推荐答案

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

First of all, don't worry they are not deprecating :if and :unless. They are deprecating passing a string to it.

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

Using lambda instead is way better in this case

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

这篇关于Rails:如何替换 Rails 5.2 的 :if 和 :unless 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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