有条件地设置方法选项? [英] Conditionally set method options?

查看:50
本文介绍了有条件地设置方法选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法有条件地为方法设置选项?

Is there a way to conditionally set options for a method?

有点像……

article_url(@article, :host => 'myblog.com' if @user.custom_domain?)

所以在这种情况下...只有在用户为其帐户设置了某个选项时才设置 host.

So in that case...only setting host if the user has a certain option set for their account.

推荐答案

opts = @user.custom_domain? ? {:host => 'myblog.com'} : {}
article_url @article, opts

或单衬:

article_url @article, @user.custom_domain? ? {:host => 'myblog.com'} : {}

这篇关于有条件地设置方法选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆