2013-10-11 30 views
1

我有我的轨道4应用程序中的这个ActiveModel。acts_as_api未定义的本地变量与ActiveModel ::模型

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 

我把这种模式形成我的控制器,这返回一个错误:

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

所以,可能的下加载ActiveModel打电话acts_as_api? 如果是的话,你能告诉我这是怎么回事?

在此先感谢

更清楚,我的问题是不是静态变量acts_as_api,与宝石“acts_as_api”,如果你想使用里面的常量不被加载ActiveModel

回答

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 

您可以在维基找到一个例子:

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

如果您需要进一步的帮助,只是让我知道。 :)

+0

嗨@chris_b,感谢您的回复,这项工作就像一个魅力。 我已经发布了关于acts_as_api的另一个问题,这是[link](http://stackoverflow.com/questions/19356290/format-xml-with-properties-with-acts-as-api),你能帮我吗? – cingusoft

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

+0

嗨,谢谢你的回复。 我想这不清楚发生了什么,我的错。问题在于,ActiveModel中无法识别“gem acts_as_api”。 这是该项目的链接更清晰: [act_as_api](http://fabrik42.github.io/acts_as_api/) – cingusoft