在rails中测试params的最佳方式是什么?

时间:2022-10-30 20:08:06

Well, I have a params hash like this:

我有一个params散列

params = { :q => { :id_eq => "" } }

What is the best way to test if id_eq is blank? I'm currently using if params[:q] && params[:q][:id_eq].blank? but it seems a lot of duplicated code.

如果id_eq为空,最好的测试方法是什么?我现在使用if params[:q] & params[:q][:::id_eq].blank?但似乎有很多重复的代码。

Is there a better way?

有更好的方法吗?

3 个解决方案

#1


0  

if params.fetch(:q, {id_eq: true})[:id_eq].blank?


Comparison between the OP's original code and other answers. So far, my answer is the only one that gives the correct result.

params = { :q => { :id_eq => "" } }

params[:q] && params[:q][:id_eq].blank?         # => true (OP)
params.fetch(:q, {id_eq: true})[:id_eq].blank?  # => true (my code)
params[:q].try(:[], :id_eq).blank?              # => true (Marek Lipka)
params.try(:values_at, :q).first[:id_eq].blank? # => true (mbratch old)
params[:q][:id_eq].blank? rescue false          # => true (Doydle)
params.fetch(:q,{})[:id_eq].blank?              # => true (Stefan)
params.try(:[], :q).try(:[], :id_eq).blank?     # => true (mbratch new)

params = { :q => { :id_eq => "foo" } }

params[:q] && params[:q][:id_eq].blank?         # => false
params.fetch(:q, {id_eq: true})[:id_eq].blank?  # => false
params[:q].try(:[], :id_eq).blank?              # => false
params.try(:values_at, :q).first[:id_eq].blank? # => false
params[:q][:id_eq].blank? rescue false          # => false
params.fetch(:q,{})[:id_eq].blank?              # => false
params.try(:[], :q).try(:[], :id_eq).blank?     # => false

params = {}

params[:q] && params[:q][:id_eq].blank?         # => nil   (falesy value)
params.fetch(:q, {id_eq: true})[:id_eq].blank?  # => false (falesy value)
params[:q].try(:[], :id_eq).blank?              # => true  # Wrong
params.try(:values_at, :q).first[:id_eq].blank? # => true  # Wrong
params[:q][:id_eq].blank? rescue false          # => true  # Wrong
params.fetch(:q,{})[:id_eq].blank?              # => true  # Wrong
params.try(:[], :q).try(:[], :id_eq).blank?     # => true  # Wrong 

For the purpose of conditional, the difference between nil and false would not matter, but getting true instead of nil is surely wrong.

出于条件的目的,nil和false之间的区别并不重要,但是用true代替nil肯定是错误的。

#2


4  

There is no better way than how you're already doing it. (At least in my opinion.) Relying on exceptions is a bad idea and anything else is significantly less legible.

没有比你已经在做这件事更好的方法了。(至少在我看来是这样。)依赖于例外是一个坏主意,其他任何东西都明显地难以辨认。

#3


-1  

rescue is always an option:

救援总是一个选择:

if (params[:q][:id_eq].blank? rescue false)

#1


0  

if params.fetch(:q, {id_eq: true})[:id_eq].blank?


Comparison between the OP's original code and other answers. So far, my answer is the only one that gives the correct result.

params = { :q => { :id_eq => "" } }

params[:q] && params[:q][:id_eq].blank?         # => true (OP)
params.fetch(:q, {id_eq: true})[:id_eq].blank?  # => true (my code)
params[:q].try(:[], :id_eq).blank?              # => true (Marek Lipka)
params.try(:values_at, :q).first[:id_eq].blank? # => true (mbratch old)
params[:q][:id_eq].blank? rescue false          # => true (Doydle)
params.fetch(:q,{})[:id_eq].blank?              # => true (Stefan)
params.try(:[], :q).try(:[], :id_eq).blank?     # => true (mbratch new)

params = { :q => { :id_eq => "foo" } }

params[:q] && params[:q][:id_eq].blank?         # => false
params.fetch(:q, {id_eq: true})[:id_eq].blank?  # => false
params[:q].try(:[], :id_eq).blank?              # => false
params.try(:values_at, :q).first[:id_eq].blank? # => false
params[:q][:id_eq].blank? rescue false          # => false
params.fetch(:q,{})[:id_eq].blank?              # => false
params.try(:[], :q).try(:[], :id_eq).blank?     # => false

params = {}

params[:q] && params[:q][:id_eq].blank?         # => nil   (falesy value)
params.fetch(:q, {id_eq: true})[:id_eq].blank?  # => false (falesy value)
params[:q].try(:[], :id_eq).blank?              # => true  # Wrong
params.try(:values_at, :q).first[:id_eq].blank? # => true  # Wrong
params[:q][:id_eq].blank? rescue false          # => true  # Wrong
params.fetch(:q,{})[:id_eq].blank?              # => true  # Wrong
params.try(:[], :q).try(:[], :id_eq).blank?     # => true  # Wrong 

For the purpose of conditional, the difference between nil and false would not matter, but getting true instead of nil is surely wrong.

出于条件的目的,nil和false之间的区别并不重要,但是用true代替nil肯定是错误的。

#2


4  

There is no better way than how you're already doing it. (At least in my opinion.) Relying on exceptions is a bad idea and anything else is significantly less legible.

没有比你已经在做这件事更好的方法了。(至少在我看来是这样。)依赖于例外是一个坏主意,其他任何东西都明显地难以辨认。

#3


-1  

rescue is always an option:

救援总是一个选择:

if (params[:q][:id_eq].blank? rescue false)