开发者

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'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜