Vadim B
Vadim B

Reputation: 73

DEPRECATION WARNING: Method length is deprecated and will be removed in Rails 5.1

Faced with such a "warning" in line of code. DEPRECATION WARNING: Method length is deprecated and will be removed in Rails 5.1, as ActionController::Parameters no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.2/classes/ActionController/Parameters.html

if params[:q].blank? && params[:advanced_search].blank? || params[:q]&.length == 1 && params[:q][:s].present?
  @q.add_default_condition('status', 'in', 'open')
  session[:last_ticket_search] = nil
end

params[:q]&.length == 1 What alternatives can be used?

params[:q].to_unsafe_h&.length == 1 Can I do so?

Upvotes: 0

Views: 755

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14900

You can do the second part of the check in Rails 5.1 like this using the dig method.

params.to_unsafe_h.dig(:q, :s).present?

Using length in Rails 5.1 is a bit tricky, since the params now includes both controller and action keys, so be aware of that.

Upvotes: 1

Related Questions