How to check for the presence of no value in a "params[:attribute]"

marcamillion

When I have a an array like this:

[5] pry(#<HomeController>)> params["search"]["sources"] => [""]

How do I check that instance to be true?

I tried the following, but it fails:

[8] pry(#<HomeController>)> params[:search][:sources].empty?
=> false
[9] pry(#<HomeController>)> params[:search][:sources].nil?
=> false
[3] pry(#<HomeController>)> params["search"]["sources"].empty?
=> false
[4] pry(#<HomeController>)> params["search"]["sources"].eql? ""
=> false
[10] pry(#<HomeController>)> params[:search][:sources].blank?
=> false
[11] pry(#<HomeController>)> params[:search][:sources].any?
=> true

Whenever the value of an attribute of my params is empty, or equal to "", I want to do something specific.

I would like it to be the conditional of an if statement, so it has to return true in the above case.

Here is the actual full params I am checking:

 params
=> <ActionController::Parameters {"search"=><ActionController::Parameters {"keywords"=>"", "types"=>[""], "categories"=>["", "", "", "", "", "", "", "Hockey", "", "", "", "", "", "", "", "", "", "", "", "", ""], "date_from"=>"", "date_to"=>"", "sources"=>[""], "genders"=>[""], "ages"=>[""]} permitted: false>, "controller"=>"home", "action"=>"index"} permitted: false>

Basically I want to check to see if any of the values in params[:search][:categories] is not empty. In the above case, we see that one of the values is equal to the string Hockey, so in this case it should return false.

spickermann

I would probably try to avoid having to check for both – a blank array or an array including only an empty string. But if I had to I would to it explicit:

array = params[:search][:sources]
array.blank? || array == ['']

Another option might be:

params[:search][:sources].first.blank?

But I think the second option is harder to understand (at least all cases in which it would return true).

For the categories the condition would be:

params[:search][:categories].any?(&:present?)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to check text field presence Xcode (programmatically coded)

분류에서Dev

How to check for a value in an array

분류에서Dev

How to check only numbers in value - Lua

분류에서Dev

How to check radio button with radio button value

분류에서Dev

How to check if user input is not an int value

분류에서Dev

How to check if a value/property exist in JSON data

분류에서Dev

how to check for a null value in t-sql

분류에서Dev

Is there any way to depend presence of key in Object on external value?

분류에서Dev

How to detect the presence of mobile devices in an area using WiFi router

분류에서Dev

How to I check whether a given variable value is of type string

분류에서Dev

How to check if a ResultSet of an AJAX call using JQuery has value

분류에서Dev

How can I check for the existance of an enum value in an array of Enums?

분류에서Dev

How to check file integrity of download if checksum value is not posted

분류에서Dev

How to check a Radio button from the value of a table of mySQL

분류에서Dev

How to check Key Value Observing in ObjectiveC for dictionary of arrays?

분류에서Dev

How to check a class for a certain value and then return a result through Tag Manager

분류에서Dev

How Ubuntu/Linux defines value to force check disks/partitions on boot?

분류에서Dev

How to check if a value exists within a C++ Map

분류에서Dev

"params [: attribute]"에 값이 없는지 확인하는 방법

분류에서Dev

How to check for null widget value in dart ternary operator to display default image if null?

분류에서Dev

How to Check value id from Tag Input and remove count row is 0?

분류에서Dev

How to tick check-boxes on page load according to the value of radio button in jquery?

분류에서Dev

How do I loop through certain records, check a query, and conditionally assign field value using VBA?

분류에서Dev

jQuery to take value from input and check if it's right. If so, it should post how many are correct

분류에서Dev

How to check value of a column lies between values of two columns in other file and print corresponding value from column in Unix?

분류에서Dev

Parse to Boolean or check String Value

분류에서Dev

Check value in rows with same FK

분류에서Dev

IBM Presence Insight RpiProx

분류에서Dev

How to check my niceness?

Related 관련 기사

  1. 1

    How to check text field presence Xcode (programmatically coded)

  2. 2

    How to check for a value in an array

  3. 3

    How to check only numbers in value - Lua

  4. 4

    How to check radio button with radio button value

  5. 5

    How to check if user input is not an int value

  6. 6

    How to check if a value/property exist in JSON data

  7. 7

    how to check for a null value in t-sql

  8. 8

    Is there any way to depend presence of key in Object on external value?

  9. 9

    How to detect the presence of mobile devices in an area using WiFi router

  10. 10

    How to I check whether a given variable value is of type string

  11. 11

    How to check if a ResultSet of an AJAX call using JQuery has value

  12. 12

    How can I check for the existance of an enum value in an array of Enums?

  13. 13

    How to check file integrity of download if checksum value is not posted

  14. 14

    How to check a Radio button from the value of a table of mySQL

  15. 15

    How to check Key Value Observing in ObjectiveC for dictionary of arrays?

  16. 16

    How to check a class for a certain value and then return a result through Tag Manager

  17. 17

    How Ubuntu/Linux defines value to force check disks/partitions on boot?

  18. 18

    How to check if a value exists within a C++ Map

  19. 19

    "params [: attribute]"에 값이 없는지 확인하는 방법

  20. 20

    How to check for null widget value in dart ternary operator to display default image if null?

  21. 21

    How to Check value id from Tag Input and remove count row is 0?

  22. 22

    How to tick check-boxes on page load according to the value of radio button in jquery?

  23. 23

    How do I loop through certain records, check a query, and conditionally assign field value using VBA?

  24. 24

    jQuery to take value from input and check if it's right. If so, it should post how many are correct

  25. 25

    How to check value of a column lies between values of two columns in other file and print corresponding value from column in Unix?

  26. 26

    Parse to Boolean or check String Value

  27. 27

    Check value in rows with same FK

  28. 28

    IBM Presence Insight RpiProx

  29. 29

    How to check my niceness?

뜨겁다태그

보관