我为acts_model编写了一个代码,其中包含acts_as_api。代码如下。它从不提取日期。我做错了什么。代码如下
acts_as_api
api_accessible :bill_corrections do |inv|
inv.add :common_date, :as => :recieved_date, :if => lambda{|u|u.date_type_code=="RECVD"}
inv.add :common_date, :as => :actual_date, :if => lambda{|u|u.date_type_code=="ARRIV"}
end
我是datefield的架构
create_table "item_dates", :force => true do |t|
t.integer "item_date_id", :limit => 10
t.string "date_type_code"
t.datetime "common_date"
t.datetime "created_at"
t.datetime "updated_at"
end
答案 0 :(得分:1)
我是acts_as_api的创建者。我用最小的Rails应用程序尝试了你的用例,它对我有用。以下是xml中的示例响应:
<users type="array">
<user>
<first-name>Me</first-name>
<recieved-date type="datetime">2011-10-07T08:20:02Z</recieved-date>
</user>
<user>
<first-name>Me2</first-name>
<actual-date type="datetime">2011-10-07T08:20:52Z</actual-date>
</user>
</users>
模型中的api模板如下所示:
api_accessible :test_dates do |t|
t.add :first_name
t.add :common_date, :as => :recieved_date, :if => lambda{|u|u.date_type_code=="RECVD"}
t.add :common_date, :as => :actual_date, :if => lambda{|u|u.date_type_code=="ARRIV"}
end
这是User.all的转储:
[
[0] #<User:0x0000010b859308> {
:id => 1,
:first_name => "Me",
:last_name => "Too",
:age => nil,
:active => nil,
:created_at => Fri, 07 Oct 2011 08:20:19 UTC +00:00,
:updated_at => Fri, 07 Oct 2011 08:20:19 UTC +00:00,
:date_type_code => "RECVD",
:common_date => Fri, 07 Oct 2011 08:20:02 UTC +00:00
},
[1] #<User:0x0000010b858688> {
:id => 2,
:first_name => "Me2",
:last_name => "Too2",
:age => nil,
:active => nil,
:created_at => Fri, 07 Oct 2011 08:20:45 UTC +00:00,
:updated_at => Fri, 07 Oct 2011 08:20:53 UTC +00:00,
:date_type_code => "ARRIV",
:common_date => Fri, 07 Oct 2011 08:20:52 UTC +00:00
}
]
您是否检查过正确使用控制器助手?
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render_for_api :test_dates, :xml => @users, :root => :users }
format.json { render_for_api :test_dates, :json => @users, :root => :users }
end
end
我希望这会有所帮助。 :)
进一步调试可能包括: *如果删除:if选项会怎样? *如果你把它放在:if options block中,那么'put u.date_type_code'会说什么?