Gugubaight Gugubaight - 1 year ago 83
Ruby Question

Rails 4: Looping through Arrays

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

no implicit conversion of nil into String

if I put this in my View (haml):


- ["Greg", "is", "cool", "ya"].each do |tag|
= 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 Source

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