我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选项块中,'puts u.date_type_code'是什么意思?
“提取”是指“加载”还是“返回”?你有没有检查你的条件是否真的实现了?您是否尝试过返回其他字段以查看它是否返回任何内容? – polarblau
@polarblau,我的意思是回报。除日期API外,所有其他字段都会返回。我在这里粘贴的是acts_as_api回复的一部分。我有其他约11个模型使用相同的acts_as_api响应。他们都得到了答复。只有来自日期的api不会获取结果。 –