未定义的方法`[]'的零:NilClass对新的Rails模式 [英] undefined method `[]' for nil:NilClass for Rails model on new

查看:108
本文介绍了未定义的方法`[]'的零:NilClass对新的Rails模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,学生和控制器学生。在控制器我有一个新的方法

高清新   @student = Student.new 结束

然后我有一个/views/students/new.html.haml的怪胎使用@student。我得到的错误

 未定义的方法`[]'的零:NilClass
 

这是HAML它

 %H1学生#新
%P找到我在app / views /学生/ new.html.haml

=的form_for(@student)做| F |
  = f.text_field:电子邮件
  = f.button提交
 

如果我更换@student与Student.new我得到相同的结果。

但是,如果我开始了新的服务器,只是使用

  = Student.new
 

我会得到这个输出

 #<学生:0x007fdf7a9c0778>
 

如果我进入轨道控制台我可以摆弄学生使用Student.new和Student.all等。

有没有人有任何想法发生了什么或什么,我做错了什么?

下面是当我加载本地主机的错误日志:3000 /学生/新

 入门使用/学生/新为127.0.0.1,在2014年1月22日二十点53分50秒-0600
处理由StudentsController#新的HTML
在1毫秒完成500内部服务器错误

NoMethodError  - 未定义的方法`[]'的零:NilClass:
ActiveRecord的(4.0.2)的lib / active_record / attribute_methods / read.rb:84:在'read_attribute
ActiveRecord的(4.0.2)的lib / active_record / attribute_methods / read.rb:59:在'__temp__36c6163737
ActiveRecord的(4.0.2)的lib / active_record / core.rb:171:在'初始化'
ActiveRecord的(4.0.2)的lib / active_record / inheritance.rb:27:在'新'
应用程序/控制器/ students_controller.rb:3:在'新​​'
ActionPack的(4.0.2)的lib / action_controller /金属/ implicit_render.rb:4:`send_action
ActionPack的(4.0.2)的lib / abstract_controller / base.rb:189:在'process_action
ActionPack的(4.0.2)的lib / action_controller /金属/ rendering.rb:10:`process_action
ActionPack的(4.0.2)的lib / abstract_controller / callbacks.rb:18:在`块process_action
的ActiveSupport(4.0.2)的lib / active_support / callbacks.rb:383:在'_run__289605414020287583__process_action__callbacks
的ActiveSupport(4.0.2)的lib / active_support / callbacks.rb:80:在'run_callbacks
ActionPack的(4.0.2)的lib / abstract_controller / callbacks.rb:17:在'process_action
ActionPack的(4.0.2)的lib / action_controller /金属/ rescue.rb:29:在'process_action
ActionPack的(4.0.2)的lib / action_controller /金属/ instrumentation.rb:31:在`块process_action
的ActiveSupport(4.0.2)的lib / active_support / notifications.rb:159:在'块仪表
的ActiveSupport(4.0.2)的lib / active_support /通知/ instrumenter.rb:20:在'工具'
的ActiveSupport(4.0.2)的lib / active_support / notifications.rb:159:在'工具'
ActionPack的(4.0.2)的lib / action_controller /金属/ instrumentation.rb:30:在'process_action
ActionPack的(4.0.2)的lib / action_controller /金属/ params_wrapper.rb:245:在'process_action
ActiveRecord的(4.0.2)的lib / active_record / railties / controller_runtime.rb:18:在'process_action
ActionPack的(4.0.2)的lib / abstract_controller / base.rb:136:在'过程'
ActionPack的(4.0.2)的lib / abstract_controller / rendering.rb:44:在'过程'
ActionPack的(4.0.2)的lib / action_controller / metal.rb:195:在'调度'
ActionPack的(4.0.2)的lib / action_controller /金属/ rack_delegation.rb:13:在'调度'
ActionPack的(4.0.2)的lib / action_controller / metal.rb:231:在'挡在行动
ActionPack的(4.0.2)的lib / action_dispatch /路由/ route_set.rb:80:在'调度'
ActionPack的(4.0.2)的lib / action_dispatch /路由/ route_set.rb:48:在'叫'
ActionPack的(4.0.2)的lib / action_dispatch /旅程/ router.rb:71:在`块调用
ActionPack的(4.0.2)的lib / action_dispatch /旅程/ router.rb:59:在'叫'
ActionPack的(4.0.2)的lib / action_dispatch /路由/ route_set.rb:680:在'叫'
机架(1.5.2)的lib /架/ etag.rb:23:在'叫'
机架(1.5.2)的lib /架/ conditionalget.rb:25:在'叫'
机架(1.5.2)的lib /架/ head.rb:11:在'叫'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ params_parser.rb:27:在'呼'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ flash.rb:241:在'呼'
机架(1.5.2)的lib /架/会话/抽象/ id.rb:225:在'背景'
机架(1.5.2)的lib /架/会话/抽象/ id.rb:220:在'呼'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ cookies.rb:486:在'呼'
ActiveRecord的(4.0.2)的lib / active_record / query_cache.rb:36:在'呼'
ActiveRecord的(4.0.2)的lib / active_record / connection_adapters /抽象/ connection_pool.rb:626:在'叫'
ActiveRecord的(4.0.2)的lib / active_record / migration.rb:369:在'呼'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ callbacks.rb:29:在`块调用
的ActiveSupport(4.0.2)的lib / active_support / callbacks.rb:373:在'_run__3101080685994121215__call__callbacks
的ActiveSupport(4.0.2)的lib / active_support / callbacks.rb:80:在'run_callbacks
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ callbacks.rb:27:在'呼'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ reloader.rb:64:在'呼'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ remote_ip.rb:76:在'呼'
better_errors(1.0.1)的lib / better_errors / middleware.rb:84:在'protected_app_call
better_errors(1.0.1)的lib / better_errors / middleware.rb:79:在'better_errors_call
better_errors(1.0.1)的lib / better_errors / middleware.rb:56:在'叫'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ debug_exceptions.rb:17:在'呼'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ show_exceptions.rb:30:在'呼'
railties(4.0.2)的lib /导轨/机架/ logger.rb:38:在'call_app
railties(4.0.2)的lib /导轨/机架/ logger.rb:20:在`块调用
的ActiveSupport(4.0.2)的lib / active_support / tagged_logging.rb:67:在`块标记
的ActiveSupport(4.0.2)的lib / active_support / tagged_logging.rb:25:在'标签'
的ActiveSupport(4.0.2)的lib / active_support / tagged_logging.rb:67:在'标签'
railties(4.0.2)的lib /导轨/机架/ logger.rb:20:在'叫'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ request_id.rb:21:在'呼'
机架(1.5.2)的lib /架/ methodoverride.rb:21:在'叫'
机架(1.5.2)的lib /架/ runtime.rb:17:在'叫'
的ActiveSupport(4.0.2)的lib / active_support /缓存/策略/ local_cache.rb:83:在'呼'
机架(1.5.2)的lib /架/ lock.rb:17:在'叫'
ActionPack的(4.0.2)的lib / action_dispatch /中间件/ static.rb:64:在'呼'
机架(1.5.2)的lib /架/ sendfile.rb:112:在'叫'
railties(4.0.2)的lib /导轨/ engine.rb:511:在'叫'
railties(4.0.2)的lib /导轨/ application.rb中:97:在'叫'
机架(1.5.2)的lib /架/ lock.rb:17:在'叫'
机架(1.5.2)的lib /架/ content_length.rb:14:在'叫'
机架(1.5.2)的lib /架/处理器/ webrick.rb:60:在'服务'
/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:138:in'服务'
/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:94:in'运行'
 

/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/server.rb:295:in`块start_thread

 开始邮报/ __ better_errors / 70107129930940 /变量为127.0.0.1,在2014年1月22日20点53分51秒-0600
 

学生模型

 班学生和LT;的ActiveRecord :: Base的
结束
 

我刚刚产生的这种模式在不久前与导轨G型学生然后用控制器轨摹控制器学生

我的移民文件看起来是这样的:

 类CreateStudents< ActiveRecord的::迁移
  高清变化
    CREATE_TABLE:学生做| T |
      t.string:电子邮件
      t.string:FIRST_NAME
      t.string:姓氏
      t.string:主要
      t.string:学期
      t.string:类
      t.integer:可用性
      t.integer:状态

      t.timestamps
     结束
   结束
 结束
 

运行 Student.new.email =富给我

  SystemStackError:堆栈层次过深
从/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/irb/workspace.rb:86
也许IRB错误!
 

解决方案

你打破东西 t.string:类

@ student.class 需要的返回学生的ActiveRecord的做它的魔力

@ student.class 是要返回。这是一个问题。

I have a model Student and a controller Students. In the controller I have a new method

def new
  @student = Student.new
end

I then have a /views/students/new.html.haml that freaks out by using the @student. I get the error

   undefined method `[]' for nil:NilClass

This is the haml for it

%h1 Students#new
%p Find me in app/views/students/new.html.haml

= form_for(@student) do |f|
  = f.text_field :email
  = f.button "Submit"

If I replace @student with Student.new I get the same results.

However if I start the server new and just use

= Student.new

I will get this output

#<Student:0x007fdf7a9c0778>

If I go into the rails console I can mess around with Students and use Student.new and Student.all and etc.

Does anyone have any idea what's going on or what I'm doing wrong?

Here is the error log from when I load localhost:3000/students/new

Started GET "/students/new" for 127.0.0.1 at 2014-01-22 20:53:50 -0600
Processing by StudentsController#new as HTML
Completed 500 Internal Server Error in 1ms

NoMethodError - undefined method `[]' for nil:NilClass:
activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:84:in `read_attribute'
activerecord (4.0.2) lib/active_record/attribute_methods/read.rb:59:in `__temp__36c6163737'
activerecord (4.0.2) lib/active_record/core.rb:171:in `initialize'
activerecord (4.0.2) lib/active_record/inheritance.rb:27:in `new'
app/controllers/students_controller.rb:3:in `new'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.2) lib/active_support/callbacks.rb:383:in `_run__289605414020287583__process_action__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.2) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__3101080685994121215__call__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (1.0.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.0.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.0.1) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.2) lib/rails/engine.rb:511:in `call'
railties (4.0.2) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'

/Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

Started POST "/__better_errors/70107129930940/variables" for 127.0.0.1 at 2014-01-22 20:53:51 -0600

Student model

class Student < ActiveRecord::Base
end

I just generated this model not too long ago with rails g model Student and then the controller with rails g controller Students

My migration file looks like this:

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.string          :email
      t.string          :first_name
      t.string          :last_name
      t.string          :major
      t.string          :semester
      t.string          :class
      t.integer         :availability
      t.integer         :status

      t.timestamps
     end
   end
 end

Running Student.new.email = "foo" gives me

SystemStackError: stack level too deep
from /Users/daltondick/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/irb/workspace.rb:86
Maybe IRB bug!

解决方案

You're breaking things with t.string :class

@student.class needs to return Student for ActiveRecord to do its magic.

Your @student.class is going to return nil. That's a problem.

这篇关于未定义的方法`[]'的零:NilClass对新的Rails模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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