在Ruby on Rails中查看没有查看 [英] Checking for nil in view in Ruby on Rails

查看:133
本文介绍了在Ruby on Rails中查看没有查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Rails一段时间,我发现自己一直在做的一件事是检查在我的视图代码中是否有一些属性或对象在我显示之前是零。我开始怀疑这是否是最好的主意。



我的理由是,由于我的应用程序依赖于用户输入意外的事情可能会发生。如果我从编程中学到一件事情,那么用户输入的程序员没有想到的是运行时错误的最大来源之一。通过检查零价值,我希望能够回避,并且让我的观点妥善处理这个问题。



事情是,我通常因为各种原因有相似的无效或无效我的模型或控制器代码中的值检查。我不会在最严格的意义上将其称为代码重复,但它似乎并不是很干燥。如果我已经检查了我的控制器中的零对象是否可以,如果我的视图只是假定对象真的不是零?对于可以显示的零的属性,对我来说每次检查都是有意义的,但对于对象本身我不知道最好的做法是什么。



以下是我正在谈论的简化但典型的例子:



控制器代码

  def show 
@item = Item.find_by_id(params [:id])

@folders = Folder.find(:all,:order =>'display_order ')

如果@item == nil或@ item.folder == nil
redirect_to(root_url)并返回
end
end

查看代码

  ;%if @item!= nil%> 
在这里显示项目的属性

<%if @ item.folder!= nil%>
<%= link_to @ item.folder.name,folder_path(@ item.folder)%>
<%end%>
<%else%>
糟糕!看起来好像有些事情发生了可怕的错误!
<%end%>

这是一个好主意还是只是愚蠢?

解决方案

您的示例代码重构:



控制器代码。 (我认为这是ItemsController)

  def show 
#如果没有找到项目,这将失败404 $ b#你可以配置rails几乎渲染任何错误404
@item = Item.find(params [:id])

#似乎没有在查看
#@folders = Folder.find(:all,:order =>'display_order')


#这不再需要了,还是应该在错误404处理程序
#if @item == nil或@ item.folder == nil
#redirect_to(root_url)并返回
#end
end

查看代码,因为控制器确保我们有@item

 #显示项目的属性

<%= item_folder_link(@item)%>

帮助代码:

 #显示链接如果项目有一个文件夹
def item_folder_link(item)
#我认为folder.name应该是一个非空字符串
#你应该正确验证如果item.folder
end

无论如何,我尽量保持视图非常简单。通常,如果我在视图中看到循环和条件,我尝试将它们重构为助手。


I've been working with Rails for a while now and one thing I find myself constantly doing is checking to see if some attribute or object is nil in my view code before I display it. I'm starting to wonder if this is always the best idea.

My rationale so far has been that since my application(s) rely on user input unexpected things can occur. If I've learned one thing from programming in general it's that users inputting things the programmer didn't think of is one of the biggest sources of run-time errors. By checking for nil values I'm hoping to sidestep that and have my views gracefully handle the problem.

The thing is though I typically for various reasons have similar nil or invalid value checks in either my model or controller code. I wouldn't call it code duplication in the strictest sense, but it just doesn't seem very DRY. If I've already checked for nil objects in my controller is it okay if my view just assumes the object truly isn't nil? For attributes that can be nil that are displayed it makes sense to me to check every time, but for the objects themselves I'm not sure what is the best practice.

Here's a simplified, but typical example of what I'm talking about:

controller code

def show
    @item = Item.find_by_id(params[:id])

    @folders = Folder.find(:all, :order => 'display_order')

    if @item == nil or @item.folder == nil
        redirect_to(root_url) and return
    end
end

view code

<% if @item != nil %>
    display the item's attributes here

    <% if @item.folder != nil %>
        <%= link_to @item.folder.name, folder_path(@item.folder) %>
    <% end %>
<% else %>
    Oops! Looks like something went horribly wrong!
<% end %>

Is this a good idea or is it just silly?

解决方案

Your example code remade:

controller code. ( I assume this is ItemsController )

def show
  # This will fail with 404 if item is not found
  # You can config rails to pretty much render anything on Error 404
  @item = Item.find(params[:id])

  # doesn't seem to be used in the view
  # @folders = Folder.find(:all, :order => 'display_order')


  # this is not needed anymore, or should be in the Error 404 handler
  #if @item == nil or @item.folder == nil
  #  redirect_to(root_url) and return
  #end
end

view code, since the controller made sure we have @item

#display the item's attributes here

<%= item_folder_link(@item) %>

helper code:

# display link if the item has a folder
def item_folder_link(item)
  # I assume folder.name should be a non-blank string
  # You should properly validate this in folder model
  link_to( item.folder.name, folder_path(item.folder) ) if item.folder
end

Anyway, I try to keep view very very simple. Usually if I see loops and conditionals in views, I try to refactor them into helpers.

这篇关于在Ruby on Rails中查看没有查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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