Rails: no implicit conversion of nil into String

12,181

It's occurring because you have a nil position on your arrays, try this

whitelist_start = self.domain.whitelists.map(&:url_start).compact
whitelist_end   = self.domain.whitelists.map(&:url_end).compact

I will show you, your array is not empty

2.3.1 :017 > test = []
 => []     
2.3.1 :019 > "string".starts_with?(*test)
 => false 

And now, your nil array position

2.3.1 :020 > test = [nil]
 => [nil] 
2.3.1 :021 > "string".starts_with?(*test)
TypeError: no implicit conversion of nil into String

In my tests, this error occur only when nil is in the first position

Share:
12,181
GoYoshi
Author by

GoYoshi

Updated on June 04, 2022

Comments

  • GoYoshi
    GoYoshi almost 2 years

    I get arrays of different urls from a database column. Both are used to apply a whitelist on a url (ex. www.example.com/test/page.html). One is for the url beginning, one for the end.

    whitelist_start = self.domain.whitelists.map(&:url_start)
    whitelist_end   = self.domain.whitelists.map(&:url_end)
    

    Before I insert my url in the database, I want to check if save_url is true. After that, the filtered url will be inserted. The url can ether start (ex. www.example.com/test) or end with an url (ex. page.html). But the whole whitelist can also be empty at the same time. One of these three things.

    save_url   = url.starts_with?(*whitelist_start) || url.ends_with?(*whitelist_end) || whitelist_start.empty? || whitelist_end.empty?
    

    I'm using the * before the array name in starts_with? and ends_with? because I want to insert all array values there. However, if one array is empty (which is totally fine) I get this error and can't proceed.

    no implicit conversion of nil into String
    

    Is there a way to solve this?