我正在尝试使用gem active_model_serializers
#app/serializers/admin_serializer.rb
class AdminSerializer < ActiveModel::Serializer
attributes :id, :email, :access_locked?
end
#app/controllers/dashboard/admins_controller.rb
def index
@search = Admin.search(params[:q])
@admins = @search.result(:distinct => true).page(params[:page]).per(10)
@page_entries_info = view_context.page_entries_info @admins
# render json: @admins
respond_to do |format|
format.html
format.js
format.json {render json: @admins}
end
end
#app/views/dashboard/admins/index.html.erb
<%= debug (ActiveModel::Serializer::Adapter.adapter_class(:json_api).new(ActiveModel::Serializer.serializer_for(@admins.first).new(@admins.first),{}).to_json) %>
<%= debug (@admins.all.map{|admin| AdminSerializer.new(admin).to_json}) %>
以上调试产生以下响应:
--- '{"data":{"id":"1","type":"admins","attributes":{"email":"tech@bluesapling.com","access_locked?":false}}}' //returned by the first debug
---
- '{"object":{"id":36,"email":"aubrey_schmitt@feeneykoch.io","created_at":"2016-03-28T05:15:17.546Z","updated_at":"2016-03-28T05:15:17.546Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":20,"email":"alysa_johnston@thompson.io","created_at":"2016-03-28T05:15:16.304Z","updated_at":"2016-03-28T05:15:16.304Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":22,"email":"kristofer.langosh@kunzeluettgen.com","created_at":"2016-03-28T05:15:16.459Z","updated_at":"2016-03-28T05:15:16.459Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":37,"email":"beryl_keler@wiza.biz","created_at":"2016-03-28T05:15:17.624Z","updated_at":"2016-03-28T05:15:17.624Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":5,"email":"wilhelmine_buckridge@crona.io","created_at":"2016-03-28T05:15:15.139Z","updated_at":"2016-03-28T05:15:15.139Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":14,"email":"edward_wisoky@corkery.net","created_at":"2016-03-28T05:15:15.838Z","updated_at":"2016-03-28T05:15:15.838Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":27,"email":"leonor@jerde.biz","created_at":"2016-03-28T05:15:16.848Z","updated_at":"2016-03-28T05:15:16.848Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":2,"email":"carley@wyman.net","created_at":"2016-03-28T05:15:14.873Z","updated_at":"2016-03-28T05:15:14.873Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":10,"email":"ervin.gleichner@cremin.org","created_at":"2016-03-28T05:15:15.527Z","updated_at":"2016-03-28T05:15:15.527Z"},"instance_options":{},"root":null,"scope":null}'
- '{"object":{"id":15,"email":"lonzo.dickens@johnscole.name","created_at":"2016-03-28T05:15:15.916Z","updated_at":"2016-03-28T05:15:15.916Z"},"instance_options":{},"root":null,"scope":null}'
在第一个调试中,我只序列化一个对象,而在第二个调试中,我正在尝试为一个对象数组执行此操作。
第一个调试正确返回对象的序列化版本(以json_api格式),而第二个调试则没有。
尝试了ArraySerializer,但没有成功:ActiveModel::Serializer::ArraySerializer.new(@admins, each_serializer: AdminSerializer).as_json
如何实现所需的序列化。而且,如果实现了,我可以使用其他一些简化版本吗?因为这个调试语句太冗长了。
尝试了此处提到的所有解决方案 - How do you initialize an ActiveModel::Serializer class with an ActiveRecord::Relation array?
我试图解决的基本问题是,在Admin控制器的索引方法中,Admin对象作为PORO传递给index.html文件。但我想要这个对象的序列化json版本,以便我可以将它作为一个道具传递给我的反应组件
索引方法在触发http://dashboard.localhost.com:3000/admins.json时渲染正确的json
索引方法的UPDATE#1
def index
@search = Admin.search(params[:q])
@admins_array = @search.result(:distinct => true).to_a
if params[:page]
@admins = @search.result(:distinct => true).page(params[:page][:number]).per(10)
@admins_json_array = Kaminari.paginate_array(@admins_array).page(params[:page][:number]).per(10)
else
@admins = @search.result(:distinct => true).page(1).per(10)
@admins_json_array = Kaminari.paginate_array(@admins_array).page(1).per(10)
end
@admins_json = ActiveModel::SerializableResource.new(@admins_json_array.to_a)
...
...
...
end
答案 0 :(得分:25)
由于需要默认序列化程序的不同属性,我需要指定序列化程序的控制器。
在控制器中:
def index
search = User.ransack(search_params)
render json: search.result, each_serializer: MembershipRenewalSerializer::MemberSerializer
end
所以,只是为了让事情有效,如果指定each_serializer
选项会发生什么?
外部控制器:
ActiveModel::SerializableResource.new(
User.first(2),
each_serializer: MembershipRenewalSerializer::MemberSerializer
).to_json
请注意,未指定each_serializer
,SerializableResource
将使用UserSerializer
。
看起来@admins数据发生了一些奇怪的事情。
尝试转换为数组:
ActiveModel::SerializableResource.new(@admins.to_a).to_json
要对数组进行分页,请尝试以下操作:
@search = Admin.search(params[:q])
@results = @search.result(:distinct => true).to_a
@admins = Kaminari.paginate_array(@results).page(params[:page]).per(10)
答案 1 :(得分:2)
按照指南:Serializing before controller render
您可以使用+--------------+
| Widgets |
+----+---------+
| id | version |
|----|---------|
| 1 | 1.3.2 | <- This is fine
| 3 | 10.1.2 | <- This should be last, since 10 > 4
| 2 | 4.5.7 | <- This should be second, since 4 < 10
+----+---------+
ActiveModel::SerializableResource.new(@admins, adapter: :json_api).to_json
中的
index.html.erb
下面是输出(使用帖子)
<%= debug (ActiveModel::SerializableResource.new(@posts, adapter: :json_api).to_json) %>
答案 2 :(得分:0)
我对某些API帮助器方法产生了疑问,您可以在其中检查其收集过程是否找到了合适的序列化程序并将其传递给收集序列化程序。
def api_response(data)
render json: wrap_answer(data)
end
def wrap_answer(data)
if data.respond_to?(:each)
ActiveModel::Serializer::CollectionSerializer.new(data, each_serializer: ActiveModel::Serializer.serializer_for(data.first))
else
data
end
end
答案 3 :(得分:0)
应该将其制成字符串,并使用json.stringify使其成为字符串,并使您的生活变得轻松