Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails nil:NilClass的Rails`update_attributes`Undefined method`first`_Ruby On Rails_Ruby On Rails 3_Update Attributes_Globalize3 - Fatal编程技术网

Ruby on rails nil:NilClass的Rails`update_attributes`Undefined method`first`

Ruby on rails nil:NilClass的Rails`update_attributes`Undefined method`first`,ruby-on-rails,ruby-on-rails-3,update-attributes,globalize3,Ruby On Rails,Ruby On Rails 3,Update Attributes,Globalize3,我试图在记录上使用update_属性,但失败了,我不知道为什么,我肯定错过了一些明显的东西,因为我已经多次使用了这种方法 我正在尝试为一个使用Globalize3作为其名称变量的模型播种数据 class City < ActiveRecord::Base attr_accessible :name translates :name end 带有_locale的定义如下: def with_locale(new_locale, &block) return if bl

我试图在记录上使用update_属性,但失败了,我不知道为什么,我肯定错过了一些明显的东西,因为我已经多次使用了这种方法

我正在尝试为一个使用
Globalize3
作为其名称变量的模型播种数据

class City < ActiveRecord::Base
  attr_accessible :name

  translates :name
end
带有_locale的
定义如下:

def with_locale(new_locale, &block)
  return if block.nil?

  locale_to_restore = I18n.locale
  I18n.locale = new_locale
  block.call
  I18n.locale = locale_to_restore
  nil
end

我终于通过追踪找到了答案

原来我的自定义方法
和_locale
也是由globalize3定义的,并导致了各种问题


多亏了
@cthulhu
,我找到了关于
I18n的信息。使用了_locale
,我想这可能是问题所在(),但我的记录似乎是可写的。更多的堆栈跟踪会有所帮助。调用
的第一个
在哪里?它在你的代码里吗?通过
3
?在rails?同意@gregates。您向我们显示的错误与在nil对象上使用对“first”方法的调用有关。在您的示例中,我没有看到对“first”的调用。完整堆栈跟踪,更多代码,或者两者都可以?我在
seed.rb
中使用此代码,所以通过使用
rake db:seed
。我只收到以下消息:
rake中止!未定义的方法
first'for nil:NilClass`@gregates谢谢,真不敢相信我没有想到这一点。要点是:酷,我正要发布这个!很高兴我们能(某种程度上)提供帮助。顺便说一句,有一种方法I18n。使用与您的方法相同的语言环境,那么为什么不使用它呢?@gregates您确实提供了帮助,如果没有您,我不会想到使用
trace
,我不知道为什么,因为我通常都这么做@我没有意识到这个方法,我会用它来摆脱我的。
def with_locale(new_locale, &block)
  return if block.nil?

  locale_to_restore = I18n.locale
  I18n.locale = new_locale
  block.call
  I18n.locale = locale_to_restore
  nil
end