mercredi 29 juillet 2020

Mutliple columns of images in table - how to get image url for each one?

I have a table in my rails API called "Visions" and within that table I have 4 columns for images:

  1. image
  2. imagetwo
  3. imagethree
  4. imagefour

I am able to successfully receive image_url of 'image' however I also want to be able to get the url for the remaining 3 image columns.

This is what I have in _vision.json.jbuilder:

json.call(
    vision,
    :id,
    :description,
    :hairdate,
    :products,
    :rating,
    :drying,
    :hairday,
    :styling,
    :created_at,
    :updated_at
)

json.image_url  polymorphic_url(vision.image)

adding json.imagetwo_url polymorphic_url(vision.imagetwo) etc to the end of the code breaks the app and prevents data being sent to my react native front-end.

vision.rb:

class Vision < ApplicationRecord
    has_one_attached :image
    has_one_attached :imagetwo
    has_one_attached :imagethree
    has_one_attached :imagefour
    belongs_to :user

    def featured_image_url
        if self.image.attachment
          self.image.attachment.service_url
        end
      end
end

visions_controller.rb:

    def index
        @visions = current_user&.visions
        render :index, status: :ok
    end

    def new
        @vision = Vision.new
    end

    def create
        @vision = current_user&.visions.create(vision_params)
        render :create, status: :created
    end

    def destroy
        @vision = current_user&.visions.where(id: params[:id]).first
        if @vision.destroy
            head(:ok)
        else
            head(:unprocessable_entity)
        end
    end

    def update
        @vision = current_user&.visions.where(id: params[:id]).first

        if @vision.update(edit_vision_params)
            head(:ok)
        else
            head(:unprocessable_entity)
        end
    end


    private

    def vision_params
        params.permit(:description, :image, :imagetwo, :imagethree, :imagefour, :hairdate, :rating, :products, :hairday, :drying, :styling)
    end
end

Aucun commentaire:

Enregistrer un commentaire