Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to replace :if and :unless option for rails 5.2

in application_controller.rb:4

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

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)

Question:

  1. What is option in :if and :unless in that clause to be rewrite
  2. And how to rewrite for compliance with rails 5.2

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

like image 505
PKul Avatar asked Oct 12 '17 22:10

PKul


People also ask

How to install rails on another version of Ruby?

You can also do it with your terminal with the command line gem list rails --remote --all | grep "^rails ". I you want to install rails on another version of ruby, you need to switch version of ruby and re-install version of rails. You can check list gems to see if you installed succesfullly:

What Rails version should I upgrade to?

Before making the big jump, make sure to upgrade to Rails 5.2.3 and that everything is running as expected at this version. If your Rails version is 5.2, it would be a painless process. That was the case for one of my apps, but the other app was at Rails 5.0.6.

How to configure cors on rails?

The easiest way to configure CORS on your Rails app is to use rack-cors gem. You can install it like any other gem, by executing: Next, you need to provide the configuration for the gem. You need to inform Rails which origin it should allow. To do that, you need to create a new initializer for your application.

What is the difference between front end and backend in rails?

In the case of a monolith Ruby on Rails application, both front end and back end are at the same origin. But when you use the Rails application only as an API, then you’ll have another application running as a front end. That’s when you’ll need to configure Rails to allow that front-end application to connect.


1 Answers

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

Using lambda instead is way better in this case

class ApplicationController < ActionController::Base
  before_action :prepare_meta_tags, if: -> { request.get? }
...
end
like image 167
khaled_gomaa Avatar answered Oct 21 '22 21:10

khaled_gomaa