我在rails和构建应用程序方面没有太多经验。我正在尝试使用ActiveModel :: Serializers构建API。
有条件地为特定事件操作侧载数据的最佳方法是什么?
我是否必须通过每次调用发送查询参数来执行此操作,或者我可以设置包括:仅对特定操作或任何其他建议设置为true?
class EventsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
before_filter :locate_collection, :only => :index
skip_before_filter :verify_authenticity_token, :only => [:create, :index, :show]
before_action :set_event, only: [:show, :edit, :update, :destroy]
# GET /events
def index
if params.has_key?("mode") and params[:mode] == "owned"
own_event
else
# @events = Event.all
render json: @events
end
end
# GET /events/1
def show
render json: @event
end
# GET /events/new
def new
@event = Event.new
end
# GET /events/1/edit
def edit
end
# POST /events
# POST /events.json
def create
@event = Event.new(event_params)
@event.creator_user_id = current_user.id
if @event.save
render json: @event
else
render json: @event.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
if @event.update(event_params)
render json: @event
else
render json: @event.errors, status: :unprocessable_entity
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
aa = @event.destroy
render json: aa
end
def own_event
@events = Event.where(creator_user_id: current_user.id)
if @events.count > 0
render json: @events
else
render json: []
end
# else
# render json: {error: 1, message: "Events not found."}, status: 404
# end
end
def locate_collection
if (params.has_key?("filter"))
@events = EventPolicy::Scope.new(current_user, Event).resolve(filtering_params)
# @event_orders = EventOrder.filter(filtering_params)
else
@events = policy_scope(Event)
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def filtering_params
params.slice(:event_type_id)
end
end
**My Event serializer**
It includes data for multiple association listed below. i don't want to show all the data with event call.
class EventSerializer < ActiveModel::Serializer
attributes :id, :event_name, :event_start_date, :event_end_date, :creator_user_id, :event_proposal_id, :daily_start_time, :daily_start_time, :payment_category, :total_capacity, :contact_email, :description, :graced_by, :contact_details, :video_url, :demand_draft_instructions, :status, :cannonical_event_id, :website, :event_type_id, :additional_details, :event_start_time, :event_end_time, :committee_id
embed :ids
has_one :address, include: true
has_many :tickets, include: true
has_many :event_cost_estimations, include: true
has_many :event_seating_category_associations, include: true
has_many :event_awarenesses, include: true
has_one :pandal_detail, include: true
has_one :bhandara_detail, include: true
has_many :event_tax_type_associations, include: true
has_many :event_team_details, include: true
has_one :event_type, include: true
# has_many :event_registration_center_associations, include: true
has_many :registration_centers, include: true
# has_many :event_registrations, include: true
has_many :event_orders, include: true
has_one :venue_type
end
In event serializer i have includes :true for sideloaded data and I want to show sideloaded data(includes: true) of registration_centers and tickets only for index action.
我能为此做些什么?
答案 0 :(得分:0)
考虑到您使用的是AMS版本0.8.x,
你可以像这样创建一个base_serializer.rb并扩展它。
class BaseSerializer < ActiveModel::Serializer
def include_associations!
if @options[:embed]
embed = @options[:embed].split(',').map!(&:to_sym)
embed.each do |assoc|
include! assoc if _associations.keys.include?(assoc)
end
end
end
end
class EventSerializer < BaseSerializer
并且EventsController中的索引方法可以写为
# GET /events?embed=registration_centers,tickets
def index
if params.has_key?("mode") and params[:mode] == "owned"
own_event
else
# @events = Event.all
render json: @events, embed: params[:embed]
end
end
我刚刚向您展示了所需的关联,可以添加到url params中。你可以想出一些聪明的方法来发送params,这样你就不需要在url请求中添加它们。