Ruby-2.6.0 と Rails 6 を使用して Ruby on Rails プロジェクトに取り組んでいます。API 部分に取り組んでおり、アプリで jsonapi-serializers gem を使用しています。シリアライザーに条件属性を追加したい。
コントローラ:
class OrderDetailsController < Api::V1::ApiApplicationController
before_action :validate_token
def show
user = User.find_by_id(@user_id)
order_details = user.order_details.where(id: params[:id])&.first
render json: JSONAPI::Serializer.serialize(order_details, context: { order_detail: true })
end
end
シリアライザー:
class OrderDetailSerializer
include JSONAPI::Serializer
TYPE = 'Order Details'
attribute :order_number
attribute :price
attribute :quantity
attribute :order_status
attribute :ordered_date, if: Proc.new { |record|
context[:order_detail] == true
}
end
ここで、コンテキスト内のコントローラーから「order_detail」を渡しています。以下のエラーが発生しています:-
TypeError (#<Proc:0x00007fe5d8501868@/app_path/app/serializers/order_detail_serializer.rb:46> is not a symbol nor a string):
jsonapi-serializerに記載されている条件付き属性に従い、「ordered_date」属性を条件付きにしようとしましたが、機能していません。
修正方法を教えてください。前もって感謝します。