编写此 if then else 子句的更好方法是什么?
我有以下代码。我仍然是 Ruby on Rails 的新手。正如你所看到的,我重复了四次。
我尝试了这样的事情:
if @property.nil? || @property.status_id == 144 || (@property.status_id <= 16 && current_user.nil?) || (@property.status_id <= 16 && current_user.id != @property.user_id)
但如果 @property 为零,它会给我很多错误。因为@property.status_id不能被调用,因为@property为零。
无论如何,我认为经验丰富的 Ruby on Rails 编码人员会明白这一点。
def show
@property = Property.find(params[:id]) rescue nil
if @property.nil?
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id == 144
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id <= 16 && current_user.nil?
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id <= 16 && current_user.id != @property.user_id
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
@images = Image.find(:all, :conditions =>{:property_id => params[:id]})
end
根
I have the following code. I'm still a newbie in Ruby on Rails. As you can see I'm repeating myself 4 times.
I tried something like this:
if @property.nil? || @property.status_id == 144 || (@property.status_id <= 16 && current_user.nil?) || (@property.status_id <= 16 && current_user.id != @property.user_id)
But it gives me lots of errors in case @property is nil. Because then @property.status_id cannnot be called since @property is nil.
Anyway, I think an experienced Ruby on Rails coder gets the idea.
def show
@property = Property.find(params[:id]) rescue nil
if @property.nil?
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id == 144
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id <= 16 && current_user.nil?
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
if @property.status_id <= 16 && current_user.id != @property.user_id
flash[:error]=t("The_property_was_not_found")
redirect_to root_path
return
end
@images = Image.find(:all, :conditions =>{:property_id => params[:id]})
end
root
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不熟悉 Ruby 语法,所以这可能无法真正编译,但你明白了。
I am not familiar with Ruby syntax so this might not really compile, but you get the idea.
这真的是确切的代码吗? || 短路 和 nil 值应该不是问题。
输出
NilClass
Is that really the exact code? || short-circuits and a nil value shouldn't be a problem.
Outputs
NilClass
我认为您应该通过将“可以显示”逻辑定义为一个简单的辅助方法来解决这个问题,您可以调用它来做出决定,而不是用最终使相同操作发生的各种分支来混乱您的显示方法。
在代码中包含像 144 这样的“神奇”数字确实会导致人们询问为什么没有为它们分配常量。当明确标记
MyApp::PROPERTY_LOCKED
时,通常更容易理解。I think you should approach this by defining the "can show" logic into a straightforward helper method you can call to make a determination rather than cluttering up your show method with all kinds of branches that ultimately make the same action occur.
Having "magic" numbers in your code like 144 does lead to asking why they aren't assigned constants. It's usually a lot easier to understand when clearly labelled
MyApp::PROPERTY_LOCKED
.你可以试试这个:
结束
You could try this:
end