在此示例中使用保护子句的正确方法是什么?
def require_admin
unless current_user && current_user.role == 'admin'
flash[:error] = "You are not an admin"
redirect_to root_path
end
end
当尝试使用这些重写时,我不知道将闪存消息放在哪里https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals惯例
您可以使用return
在此声明。本质上,该方法不需要继续下去if满足这些条件,您就可以提前退出。
def require_admin
return if current_user&.role == 'admin'
flash[:error] = 'You are not an admin'
redirect_to root_path
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)