Rails 3 ActiveModel: cannot include ActiveModel::Model directly

旧时模样 提交于 2019-12-22 05:11:52

问题


In my Rails 3.2.11 and "development" environment when I try to have an active model:

class DisponibilityApi
  include ActiveModel::Model

  attr_accessor :start_time, :end_time 
  validates :start_time, :end_time, :presence => true

end

I have an error:

NameError: uninitialized constant ActiveModel::Model

But when I include it manually:

class DisponibilityApi
  extend  ActiveModel::Naming
  extend  ActiveModel::Translation
  include ActiveModel::Validations
  include ActiveModel::Conversion

  attr_accessor :start_time, :end_time 
  validates :start_time, :end_time, :presence => true

end

Now it works !

Am I missing something ?

Thanks !


回答1:


ActiveModel::Model is new for Rails 4, which is why it shows up on Github master, but not in the 3.x gems. If you look in the 3.x version branches on Github it is not there either.

https://github.com/rails/rails/tree/3-2-stable/activemodel/lib/active_model

For Rails 3.x you will need to include each of the modules manually.

To see what it includes, check out the file in the master branch.

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/model.rb




回答2:


It seems that the ActiveModel::Model module does not exist anymore, you have to include manually the modules you want for your model.

Even if the doc states that the module still exists, a quick look in the ~/.rvm folder proves that there is no model.rb file anymore :

activemodel-3.2.11/lib » pwd                   
/Users/Intrepidd/.rvm/gems/ruby-1.9.3-p327-turbo/gems/activemodel-3.2.11/lib
activemodel-3.2.11/lib » ls
active_model    active_model.rb
activemodel-3.2.11/lib » ls -l active_model
total 280
-rw-r--r--   1 Intrepidd  staff  16574  9 Jan 00:39 attribute_methods.rb
-rw-r--r--   1 Intrepidd  staff   4556  9 Jan 00:39 callbacks.rb
-rw-r--r--   1 Intrepidd  staff   2338  9 Jan 00:39 conversion.rb
-rw-r--r--   1 Intrepidd  staff   4879  9 Jan 00:39 dirty.rb
-rw-r--r--   1 Intrepidd  staff  12087  9 Jan 00:39 errors.rb
-rw-r--r--   1 Intrepidd  staff   5259  9 Jan 00:39 lint.rb
drwxr-xr-x   3 Intrepidd  staff    102  9 Jan 00:39 locale
drwxr-xr-x   4 Intrepidd  staff    136  9 Jan 00:39 mass_assignment_security
-rw-r--r--   1 Intrepidd  staff   8720  9 Jan 00:39 mass_assignment_security.rb
-rw-r--r--   1 Intrepidd  staff   6478  9 Jan 00:39 naming.rb
-rw-r--r--   1 Intrepidd  staff   4257  9 Jan 00:39 observer_array.rb
-rw-r--r--   1 Intrepidd  staff   8163  9 Jan 00:39 observing.rb
-rw-r--r--   1 Intrepidd  staff     38  9 Jan 00:39 railtie.rb
-rw-r--r--   1 Intrepidd  staff   2939  9 Jan 00:39 secure_password.rb
-rw-r--r--   1 Intrepidd  staff   4304  9 Jan 00:39 serialization.rb
drwxr-xr-x   4 Intrepidd  staff    136  9 Jan 00:39 serializers
-rw-r--r--   1 Intrepidd  staff    319  9 Jan 00:39 test_case.rb
-rw-r--r--   1 Intrepidd  staff   2339  9 Jan 00:39 translation.rb
drwxr-xr-x  13 Intrepidd  staff    442  9 Jan 00:39 validations
-rw-r--r--   1 Intrepidd  staff   7961  9 Jan 00:39 validations.rb
-rw-r--r--   1 Intrepidd  staff   6227  9 Jan 00:39 validator.rb
-rw-r--r--   1 Intrepidd  staff    172  9 Jan 00:39 version.rb

That's funny because this file is still present on github but not in the .gem.



来源:https://stackoverflow.com/questions/14737289/rails-3-activemodel-cannot-include-activemodelmodel-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!