2013-10-11 32 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