ruby on rails - dealing with variables not being set or not existing
I was wondering if there is a better way to check if a variable exists, currently I do this
if !params['attribute']['institution'].blank?
But 开发者_JAVA技巧if attribute doesn't exist then a error is thrown.
I saw .try()
but couldn't see how it would work in this situation.
You can use present?
or presence
which was recently described by a blog post by Ola Bini.
if params['attribute'] && params['attribute']['institution']
Not the prettiest, but works.
You can use if params['attribute'].has_key? 'institution'
精彩评论