0

My goal is an output like this (for each attachment):

url: "/uploads/attachment/picture/15/5ee306c9-e263-466b-b56d-1c7c9d2ae17b.jpg"

What I have at the moment:

json object in object

attachments_controller.rb

class AttachmentsController < ApplicationController
  before_action :logged_in_user

  def index
    @attachments = current_user.attachments.all
    respond_to do |format|
      format.json do
        render :json => @attachments.each{ |o| o.picture.url }
      end
    end
  end

...
2

1 Answer 1

1

Try

respond_to do |format|
  format.json do
    render :json => @attachments.map { |o| { url: o.picture.url } }
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

This is the output: [{"url":"/uploads/attachment/picture/7/df3c0c3c-518a-44cc-afab-64dc6acb063c.jpg"},{"url":"/uploads/attachment /picture/12/dd7839ee-94c1-4390-aa5d-a37cccbc3c8a.jpg"} ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.