【问题标题】:Rails Error: undefined method `attribute_methods_generated?' for nil:NilClassRails 错误:未定义的方法“attribute_methods_generated?”对于零:NilClass
【发布时间】:2012-01-03 13:51:35
【问题描述】:

错误:

undefined method `attribute_methods_generated?' for nil:NilClass

代码是允许某人注册的简单代码。在控制器中创建了@tutor 对象。每当我删除 form_for 时,页面加载正常,但似乎没有其他方法可以修复它。

代码:

<% form_for(@tutor) do |f| %>

<table class="form_table" id="home_form">
    <tr>
        <td>First Name:</td>
        <td class="form">
            <%= f.text_field :fname, :class => 'form', :size => 30 %>
        </td>
    </tr>
    <tr>
        <td>Last Name:</td>
        <td class="form">
            <%= f.text_field :lname, :class => 'form', :size => 30 %>
        </td>
    </tr>
    <tr>
        <td>Email:</td>
        <td class="form">
            <%= f.text_field :email, :class => 'form', :size => 30 %>
        </td>
    </tr>
    <tr>
        <td>Password:</td>
        <td class="form">
            <%= f.password_field :password, :class => 'form', :size => 20 %>
        </td>
    </tr>
]
            <td>&nbsp;</td>
    <td>
        <div align="left">
            <%= f.hidden_field :branch_id, :value => @branch.id.to_s %>
            <%= f.submit 'Submit', :class => 'submit', :style => 'margin-left:3px;' %>
        </div>
    </td>
    </tr>
</table>
<% end %>

这是堆栈跟踪:

activesupport (3.0.10) lib/active_support/whiny_nil.rb:48:in `method_missing'
activerecord (3.0.10) lib/active_record/attribute_methods.rb:51:in `respond_to?'
actionpack (3.0.10) lib/action_view/helpers/form_helper.rb:329:in     `apply_form_for_options!'
actionpack (3.0.10) lib/action_view/helpers/form_helper.rb:313:in `form_for'
app/views/tutors/register.html.erb:3:in      `_app_views_tutors_register_html_erb__142383781_2257394860_0'
actionpack (3.0.10) lib/action_view/template.rb:135:in `send'
actionpack (3.0.10) lib/action_view/template.rb:135:in `render'
activesupport (3.0.10) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.10) lib/action_view/template.rb:127:in `render'
actionpack (3.0.10) lib/action_view/render/rendering.rb:59:in `_render_template'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.10) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.10) lib/action_view/render/rendering.rb:56:in `_render_template'
actionpack (3.0.10) lib/action_view/render/rendering.rb:26:in `render'
actionpack (3.0.10) lib/abstract_controller/rendering.rb:115:in `_render_template'
actionpack (3.0.10) lib/abstract_controller/rendering.rb:109:in `render_to_body'
actionpack (3.0.10) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
actionpack (3.0.10) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
actionpack (3.0.10) lib/abstract_controller/rendering.rb:102:in `render_to_string'

这里是辅导班:

 class Tutor < ActiveRecord::Base
    belongs_to :branch


 end

【问题讨论】:

  • 您是否 100% 确定 @tutor 在表格中不是 nil ?这可能就是它这样做的原因。此外,您的代码中有一个随机的 ] 字符,大约在 3/4 处。
  • 这个错误的堆栈跟踪是什么?我不明白它是如何发生的。
  • 我确定@tutor 不是零。刚刚添加了上面的堆栈跟踪。
  • 家教班长什么样?
  • 里面真的什么都没有。我完全被这个难住了。

标签: ruby-on-rails-3


【解决方案1】:

想通了。我有“类”作为表中的行之一。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多