1

i have this ActiveModel in my rails 4 app.

class MyModeltest
    include ActiveModel::Model
    acts_as_api
    attr_accessor :title, :content

    api_accessible :public do |template|
        template.add :title
        template.add :content
    end
end

i call this model form my controller and this return an error:

undefined local variable or method `acts_as_api' for ...

So, is possible to call acts_as_api under an ActiveModel? if yes, could you show me how do this?

thanks in advance

To be more clear, my problem is not with static variable acts_as_api, is with the gem "acts_as_api" that is not recognized by the ActiveModel

4

2 回答 2

3

我是acts_as_api 的作者。

如果您想将它与非ActiveRecord类一起使用,则必须使用 扩展您的类extend ActsAsApi::Base

在你的情况下,这将是

class MyModeltest
  include ActiveModel::Model
  extend ActsAsApi::Base
  acts_as_api
  attr_accessor :title, :content

  api_accessible :public do |template|
    template.add :title
    template.add :content
  end
end

您可以在 wiki 中找到一个示例:

https://github.com/fabrik42/acts_as_api/wiki/Declaring-api-templates-for-any-class%21-%28no-orms%29

如果您需要进一步的帮助,请告诉我。:)

于 2013-10-12T16:19:28.773 回答
1

如果要在模型中使用常量,则必须将其设为大写并对其进行初始化。将其定义为

class MyModeltest
    include ActiveModel::Model
    ACTS_AS_API = false
    attr_accessor :title, :content

    api_accessible :public do |template|
        template.add :title
        template.add :content
    end
end

并在类使用之外调用它,MyModeltest::ACTS_AS_API

于 2013-10-11T10:18:45.543 回答