我是行为制造者。 我用最低限度的铁路机对你的使用进行了尝试,并为我工作。 例如,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_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
而这是用户的垃圾堆放场:
[
[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
}
]
Did you check that you use the controller helpers correctly?
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
我希望这一帮助:
Further debugging could include:
* What happens if you remove the :if options?
* What does puts u.date_type_code say if you put it in the :if options blocks?