0

I want to implement method in ruby to load images from the backend app but I don't know how. I have been trying with image_tag directly from a view to call the location /image/{imageId} or /image/{documentId} but no success obviously. This is what I get from the backend api:

[
  {
    "imageId": "1",
    "srcDocumentId": 10000,
    "scopes": null,
    "teiId": "",
    "name": "image",
    "headline": "Fig. 1. ",
    "description": "Fig. 1. GPA",
    "inBody": true,
    "image": "a long string",
    "internalUrl": "image.png",
    "source": "<figure xmlns=\"http://www.tei-c.org/ns/1.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xml:id=\"fig_3\"><head>Fig. 1. </head><figDesc>Fig. 1. GPA</figDesc><graphic coords=\"4,130.95,64.62,360.00,502.42\" type=\"bitmap\" url=\"image.png\"/></figure>",
    "ord": 0
  }
]

Any help would be appreciated. Even if you tell me only the logic on how should I handle this in a model and then at the view.

Edit:

Let's say I want to show the headline and description of the image first. I have this in document_helper.rb :

  def images_format(image)
    m = []
    m << [['Description:', image.description]] if image.description
    m << [
        ['Headline', image.headline]
    ] if image.headline.any?

    m
  end

in document.rb I have this:

 class Image
    include Mongoid::Document

    field :description, type: String

    field :headline, type: String

    field :image, type: String

    field :imageId, type: String

    field :image, type: String

    field :srcDocumentId, type: Integer

    field :internalUrl, type: String

    field :name , type: String

    field :internalUrl, type: String

    field :source, type: String

  end

and also this:

 embeds_one :image, class_name: 'Document::Image'

  embeds_one :image, class_name: 'Document::Image'

and in the view document_show.haml I put:

.document__body= images_format(@document.image)

I get this error: undefined method 'description' for nil:NilClass

ga4696
  • 139
  • 11
  • Usually starts with `JSON.load` and goes from there. How far did you get? – tadman Sep 25 '18 at 21:34
  • @tadman not that far. I have created a class inside `document.rb` file called `class Image` and included `include Mongoid::Document` then `field :imageId, type: String` `field :image, type: String` and so on.. – ga4696 Sep 25 '18 at 21:41
  • Is `"a long string"` a base64 encoded image? if so, take a look here: [How to display Base64 images in HTML?](https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html) – max pleaner Sep 26 '18 at 01:28
  • @maxpleaner yes it's a base64. Do you know how can I put a link instead of the string here? `Red dot` – ga4696 Sep 28 '18 at 20:36
  • @Gemza I don't know what you mean "how I can put a link instead of the string here". You want to put a URL inside the src tag? You would need to upload the image somewhere and get a URL. – max pleaner Sep 28 '18 at 23:03
  • @maxpleaner okay let's say it like this I want to get this image response ("this long string") from the backend dynamically instead of writing it manually bcz I might have different images for different docs. I mean how can I extract it from the backend refering to imageId since each imageId has a different base64 endoded image. – ga4696 Sep 28 '18 at 23:44
  • @gemza it depends on how it's being stored. it would be better if you can show your attempt so far ... just saying "how do i do it" is really vague – max pleaner Sep 29 '18 at 01:07
  • @maxpleaner okay, let's say I want to first display the description of the image and the headline. I will add the code by editing my initial post. thanks! – ga4696 Sep 29 '18 at 22:29
  • @Gemza see here https://stackoverflow.com/questions/16163353/display-base64-encoded-image-in-rails – max pleaner Sep 30 '18 at 18:40
  • @maxpleaner my problem is that I can't manage to send the base64 image to my controller – ga4696 Sep 30 '18 at 22:36

0 Answers0