Ruby on rails 4 获得;未定义的方法“[]';对于nil:NilClass“;创建记录时
我正在将一个旧的Rails 3应用程序移植到Rails 4.0.15,一切似乎都很顺利,只是每当我尝试创建一个记录(甚至在Rails控制台中)时,就会出现上述错误(对于nil:NilClass,未定义的方法“[]”) 我把事情简化了一点,但我做的是创建一个新的应用程序(Ruby on rails 4 获得;未定义的方法“[]';对于nil:NilClass“;创建记录时,ruby-on-rails-4,Ruby On Rails 4,我正在将一个旧的Rails 3应用程序移植到Rails 4.0.15,一切似乎都很顺利,只是每当我尝试创建一个记录(甚至在Rails控制台中)时,就会出现上述错误(对于nil:NilClass,未定义的方法“[]”) 我把事情简化了一点,但我做的是创建一个新的应用程序(railsnewxxxx),然后从旧的应用程序复制app/和db/树。我使用了相同的数据库。routes.rb和Gemfile已手动调整 其中一种型号是Contact: class Contact < ActiveRecor
railsnewxxxx
),然后从旧的应用程序复制app/
和db/
树。我使用了相同的数据库。routes.rb和Gemfile已手动调整
其中一种型号是Contact:
class Contact < ActiveRecord::Base
attr_accessible :name, :email, :phone, :address
end
最好的情况:我希望有人看到这一点,然后说“是的,这很容易解决。你所做的就是…”
然而,如果有人能给我指出正确的方向或出发点,我会非常高兴
谢谢大家抽出时间。Rails 4中没有了“可访问属性”功能。我建议不要使用创业板,已经有很多问题的报道
不要将“protected”(受保护)推到Rails 4中,而是在Rails 3中安装gem并对强参数特性进行后端口
这绝对是一种更合适的方法。是的,这是我的“待办事项”清单。事实证明,问题出在1.0.3版受保护的_属性上。不知道具体的问题是什么,但升级到v1.0.8让我可以继续。谢谢
Loading development environment (Rails 4.1.5)
irb(main):001:0> Contact.create
NoMethodError: undefined method `[]' for nil:NilClass
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/read.rb:113:in `block in read_attribute'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/read.rb:111:in `fetch'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/read.rb:111:in `read_attribute'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/primary_key.rb:19:in `id'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:344:in `remember_transaction_record_state'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:282:in `rollback_active_record_state!'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:267:in `save'
from /home/wucolin/.gem/ruby/gems/protected_attributes-1.0.3/lib/active_record/mass_assignment_security/persistence.rb:46:in `create'
from (irb):1
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:8:in `require'
from bin/rails:8:in `<main>'