How to include Active Storage image url in attributes (Jsonapi-serializer gem)

89 views Asked by At

How to include Active Storage image url in attributes (Jsonapi-serializer gem). Can't get image file url in models.

tutor.rb

class Tutor < ApplicationRecord
  has_many :reservations, dependent: :destroy
  has_many :users, through: :reservations, dependent: :destroy
  has_one_attached :image, dependent: :destroy

  def image_url
    Rails.application.routes.url_helpers.url_for(image) if image.attached?
  end
end

tutor_serializer.rb

class TutorSerializer
 include JSONAPI::Serializer
 has_many :reservations
 attributes :id, :name, :image_url, :experience, :description, :price, :subject, :lang
end

tutors_controller.rb

class Api::V1::TutorsController < ApplicationController
  def index
    @tutors = Tutor.all
    render json: TutorSerializer.new(@tutors).serializable_hash[:data]
  end
end

tutors_controller.rb includes image_url and it is as expected see here

==============================================

reservation.rb

class Reservation < ApplicationRecord
  belongs_to :user
  belongs_to :tutor  
end

reservation_serializer.rb

class ReservationSerializer
  include JSONAPI::Serializer
  attributes :id, :city, :reservation_date, :returning_date, :tutor
  belongs_to :tutor
end

reservation_controller.rb

class Api::V1::ReservationsController < ApplicationController
  before_action :authenticate_user!
  def index
    res = current_user.reservations
    render json: ReservationSerializer.new(res)
  end
end

reservation_controller.rb don't have image_url see here

Any idea how to include image_url inside attribute property/directory?

schema.rb

ActiveRecord::Schema[7.0].define(version: 2023_04_07_081347) do
  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.bigint "record_id", null: false
    t.bigint "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.string "service_name", null: false
    t.bigint "byte_size", null: false
    t.string "checksum"
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "active_storage_variant_records", force: :cascade do |t|
    t.bigint "blob_id", null: false
    t.string "variation_digest", null: false
    t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
  end

  create_table "reservations", force: :cascade do |t|
    t.string "city"
    t.datetime "reservation_date"
    t.datetime "returning_date"
    t.bigint "user_id", null: false
    t.bigint "tutor_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["tutor_id"], name: "index_reservations_on_tutor_id"
    t.index ["user_id"], name: "index_reservations_on_user_id"
  end

  create_table "tutors", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.integer "price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "experience"
    t.string "subject"
    t.string "lang"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "jti", null: false
    t.integer "role", default: 1
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["jti"], name: "index_users_on_jti", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
  add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
  add_foreign_key "reservations", "tutors"
  add_foreign_key "reservations", "users"
end
0

There are 0 answers