[英]Rails undefined method `[]' for nil:NilClass
The following is my current code. 以下是我目前的代码。 I removed parts not needed to understand the problem.
我删除了不需要理解问题的部分。
Setting Model 设定模型
class Setting < ActiveRecord::Base
attr_accessor :program_id, :session_types
def initialize
@session_types = Array.new
@program_id = ''
end
end
Setting Controller 设置控制器
def new
@setting = Setting.new
puts @setting.session_types.instance_of? Array
end
Setting View 设置视图
<%= form_for(@setting) do |f| %> #ERROR LINE
...
<% end %>
I get this error: 我收到此错误:
ActionView::Template::Error (undefined method `[]' for nil:NilClass):
3: </h1>
4: <p>We need to get your booking page setup. Please select the Program you wish to put up for booking.</p>
5:
6: <%= form_for(@setting) do |f| %>
7: <% @programs.each do |program| %>
8: <%= f.radio_button(:program_id, program.program_id)%>
9: <%= label_tag(:program_name, program.name) %>
app/views/settings/new.html.erb:6:in `_app_views_settings_new_html_erb__94314060479739781_70294544827460'
@setting.session_types = Empty Array
when debugging. @setting.session_types = Empty Array
调试时@setting.session_types = Empty Array
。
So I tried replacing Setting Model
with a simple Testing Model
without the array attribute as follows: 所以我尝试用一个没有数组属性的简单
Testing Model
替换Setting Model
,如下所示:
class Testing < ActiveRecord::Base
attr_accessor :program_id
end
to see if it is the Array
attribute session_types
of Setting
that is causing the error. 查看是否是导致错误的
Setting
的Array
属性session_types
。 It seems so becuase with Testing
, I get an error in the line AFTER the form_for
line, which should be correct. 看起来好像
Testing
,我在form_for
行之后的行中得到一个错误,这应该是正确的。 (the line is not needed to undestand the problem) (不需要该线来解决问题)
I think I am not understanding the initialization of an Array
attribute of a model properly. 我想我不能正确理解模型的
Array
属性的初始化。 Any help to understand why the error is thrown will be appreciated! 任何有助于理解错误抛出原因的帮助将不胜感激! Thanks in advance!
提前致谢! =D
= d
Try this 试试这个
def initialize
super
@session_types = Array.new
@program_id = ''
end
You were very scarce with pasting your errors, but that's the first thing that comes to mind. 你很难贴上你的错误,但这是我想到的第一件事。 Please post the entire error if this doesn't help.
如果这没有帮助,请发布整个错误。
The reason for that is your Setting
class inherits from ActiveRecord::Base
, calling super
first calls parent's initialize
method, this way you add functionality to the method (extend it). 原因是你的
Setting
类继承自ActiveRecord::Base
,调用super
首先调用parent的initialize
方法,这样你就可以为方法添加功能(扩展它)。 Without super
you override it, meaning you replace everything it does by default (like for example connecting to the database, validating) by your code. 如果没有
super
,则覆盖它,这意味着您可以通过代码替换它默认执行的所有操作(例如,连接到数据库,验证)。
Do not override initialize
method on ActiveRecord::Base
. 不要覆盖
ActiveRecord::Base
上的initialize
方法。 Use after_initialize
callback instead. 请改用
after_initialize
回调。 Reference possibly with your problem http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html 可能会引用您的问题http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.