ActionView::Template:Error(未定义方法 'first_name' for nil:NilClass) [英] ActionView::Template:Error(undefined method 'first_name' for nil:NilClass)

查看:47
本文介绍了ActionView::Template:Error(未定义方法 'first_name' for nil:NilClass)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户尝试将网格数据加载到表单中时,我的应用程序似乎随机抛出错误:

My app seems to randomly be throwing an error when users are trying to load grid data into the form:

   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文件

Correspondence compdata RXML file

     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

推荐答案

第一项

您似乎还有其他一些类似的问题(此处此处) 立即打开,对于当前的和当前的,它看起来像您的 @conts 值当前为 nil 意味着运行搜索时没有带回任何内容:

First item

It looks like you have a few other similar questions (here and here) open right now, and for both the current one and this one it looks like your @conts value is currently nil meaning nothing was brought back when it ran the search:

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

您确定您的表中有一个 id 等于 params[:id] 的值吗?

Are you sure there is a value available in your table with the id equal to params[:id]?

如果那里不匹配,那将是我首先查看的地方.

If there is a mismatch there, that would be the first place I would look.

您也可能会在尝试在 @conts 上调用方法 each 时遇到问题,因为 find_by_id 方法将 带回一个数组.如果您希望它遍历每个记录而不是每个键/值,请尝试使用 find_all_by_id 它将返回一个 array.

You may also run into an issue trying to call the method each on @conts because the find_by_id method will not bring back an array. If you want it to iterate over each record instead of each key/val, try using find_all_by_id which will return an array.

在查看您的其他问题后,您的语法似乎有所不同,但您不应该像这样再次迭代 @conts 吗?

After looking at your other question, it looks like your syntax on this is different, but shouldn't you iterate over @conts again like this?

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 的方式,特别是如果您打算单步执行其中的许多.

This would be the way to assign t instead of like t = @conts, especially if you plan to step through many of them.

这篇关于ActionView::Template:Error(未定义方法 'first_name' for nil:NilClass)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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