Ruby on rails 4 ruby on rails中activemodel序列化程序中的可选属性
我有以下两个序列化程序类。在index controller中,我希望跳过加载项目\产品,仅显示/编辑我希望获取项目\产品详细信息的方法Ruby on rails 4 ruby on rails中activemodel序列化程序中的可选属性,ruby-on-rails-4,active-model-serializers,rails-api,Ruby On Rails 4,Active Model Serializers,Rails Api,我有以下两个序列化程序类。在index controller中,我希望跳过加载项目\产品,仅显示/编辑我希望获取项目\产品详细信息的方法 class ProjectSerializer < ActiveModel::Serializer attributes :id, :name, :category, :project_category_id, :status, :description has_many :project_products end cl
class ProjectSerializer < ActiveModel::Serializer
attributes :id, :name, :category, :project_category_id, :status, :description
has_many :project_products
end
class ProjectProductSerializer < ActiveModel::Serializer
attributes :id, :name, :quantity
end
尝试覆盖关联方法
class ProjectSerializer < ActiveModel::Serializer
attributes :id, :name, :category, :project_category_id, :status, :description
has_many :project_products
def project_products
if current_page?(edit_path_url)
object.comments
else
object.comments = nil
end
end
class ProjectSerializer
创建序列化程序实例时,可以使用“except”参数:
respond_with @projects, except: project_products
(当索引控制器动作时)总之,您不想在show/edit中加载project_产品?您好@Defonesko就在对面我想在show/edit中加载project_产品,但不想在indexShow/edit中加载project_产品请显示您的视图和控制器代码。def index respond_with@projects endFormat of response是json谢谢您的帮助,但是当我放置代码
respond_with@projects.to_json时(:除=>[“项目产品”])
它会忽略我的活动序列化程序,并显示项目表中的所有字段。这就是您正在使用的?您是否尝试覆盖关联方法并在内部选择需要返回的内容以及路径上的条件?我只想在列出项目时从项目表中提取一些字段。并且希望产品列表与项目详细信息一起列出项目详细信息页面中的(不是所有字段)。显然使用activeModelSerializer
respond_with @projects, except: project_products