Question

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

Was it helpful?

Solution

I'm the author of acts_as_api.

If you want to use it with a non-ActiveRecord class, you have to extend your class with extend ActsAsApi::Base.

In your case this would be

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

You can find an example in the wiki:

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

If you need further assistance, just let me know. :)

OTHER TIPS

If you want to use constants inside a model then you will have to make it in uppercase and also initialize it. Define it as

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

And to call it outside the class use, MyModeltest::ACTS_AS_API

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top