ruby on rails - How to fix undefined method `split' for nil:NilClass error? -


i have following line in rails app:

@images = @product.secondary_images.split(",") 

when @product.secondary_images has content in it, runs fine. however, when there no content, error:

undefined method `split' nil:nilclass 

how can assign value @images if there no content in it?

a possible solution use try return nil in case method cannot sent secondary_images. , use or-operator assign else.

@images = @product.secondary_images.try(:split, ",") || 'some other value'   

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -