2012-01-04 31 views
0

我的应用程序似乎随机地抛出一个“ActionView :: Template:Error(undefined method`first_name'for nil:NilClass)”error正试图将网格数据加载到表单中。ActionView :: Template:Error(undefined method'first_name'for nil:NilClass)

完整的错误

ActionView::Template::Error (undefined method `first_name' for nil:NilClass): 
3:   t = @conts 
4:       xml.tag!("row",{ "id" => t.id }) do 
5: 
6:         xml.tag!("cell", t.first_name) 
7:         xml.tag!("cell", t.last_name) 
8:       xml.tag!("cell", t.email) 
9:         xml.tag!("cell", t.phone_1) 

而且下面是控制器文件

def compdata 
     @conts = Continfo.find_by_id(params[:id]) 
    end 

对应compdata RXML文件

 xml.instruct! :xml, :version=>"1.0" 
     xml.tag!("rows") do 
    t = @conts 
     xml.tag!("row",{ "id" => t.id }) do 

      xml.tag!("cell", t.first_name) 
      xml.tag!("cell", t.last_name) 
      xml.tag!("cell", t.email) 
      xml.tag!("cell", t.phone_1) 
      xml.tag!("cell", t.phone_2) 
      xml.tag!("cell", t.homepage) 
      xml.tag!("cell", t.skype) 
    end 
end 

其非常紧迫,请帮助我们修复错误..

回答

0

第一项

看起来你已经打开了几个其他的类似的问题(herehere),现在,两者的当前,这一次它看起来像你的@conts值目前nil没什么意思被带回时它跑搜索:

def compdata 
    @conts = Continfo.find_by_id(params[:id]) 
end 

你肯定有与id等于params[:id]在表中提供的值?

如果在那里有不匹配,那将是我会看的第一个地方。

第二项

您还可能会碰到试图调用方法each@conts因为find_by_id方法带回一个数组的问题。如果您希望它遍历每条记录而不是每个键/ val,请尝试使用find_all_by_id,这将返回数组

第三项

看你的其他问题后,它看起来像,这是不同的语法,但你不应该遍历@conts再这样吗?

xml.tag!("rows") do 
    @conts.each do |t| 
    xml.tag!("row",{ "id" => t.id }) do 
     xml.tag!("cell", t.first_name) 
     xml.tag!("cell", t.last_name) 
     xml.tag!("cell", t.email) 
     xml.tag!("cell", t.phone_1) 
     xml.tag!("cell", t.phone_2) 
     xml.tag!("cell", t.homepage) 
     xml.tag!("cell", t.skype) 
    end 
    end 
end 

这将是分配t,而不是像t = @conts的方式,特别是如果你打算步很多。

+0

非常感谢,新年快乐..我们在ID表中有值,但我们仍然无法从网格加载数据来形成。我们必须使用php来构建我们的应用程序,这里是我们的链接,请通过它,并告诉我如何将网格与表单连接起来,以加载形式的值,http://docs.dhtmlx.com/doku。 PHP的?编号=教程:dhtmlx_today:gridlink – user993164 2012-01-04 12:30:44

+0

是否有一个原因,你试图做到这一点与XML标签和所有这一切?在rails中,所有表单和列表都应该在生成脚手架时预先构建。 – jstim 2012-01-04 16:42:16

相关问题