Gugubaight Gugubaight - 1 month ago 16
Ruby Question

Rails 4: Looping through Arrays

I think I am doing something horribly wrong.
I get this error:

TypeError
no implicit conversion of nil into String


if I put this in my View (haml):

...

- ["Greg", "is", "cool", "ya"].each do |tag|
.tagwrapper
= link_to tag.to_s.gsub!(/[^0-9A-Za-z]/, ''), '/search?searchtags='+tag.to_s.gsub!(/[^0-9A-Za-z]/, ''), class: 'smaller half-stretched'

...


How am I supposed to do this instead?

Answer

gsub! returns nil if no substitutions were performed.

Remove the ! so it becomes:

= link_to tag.to_s.gsub(/[^0-9A-Za-z]/, ''), '/search?searchtags='+tag.to_s.gsub(/[^0-9A-Za-z]/, ''), class: 'smaller half-stretched'

APIdock Reference