开发者

ruby on rails - dealing with variables not being set or not existing

开发者 https://www.devze.com 2023-02-06 20:40 出处:网络
I was wondering if there is a better way to check if a variable exists, currently I do this if !params[\'attribute\'][\'institution\'].blank?

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'

0

精彩评论

暂无评论...
验证码 换一张
取 消