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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -