REST API - MongoDB and Json Response

Hi, i try to list all documents from MongoDB, there are 3 documents in one collection right now.

I use presenter for formatting Json response.

So far i can list only one document from Mongo collection.

/app/controllers/v1/datas_controller.rb

    class V1::DatasController < V1::ApplicationController

    def index     @datas = Data.all     render json: DataPresenter.new(Data.first)     end     end

/app/presenters/presenter.rb

    class Presenter     def initialize(object)     @data = object     end

    def as_json

    end     end

/app/presenters/data_presenter.rb

    class DataPresenter < Presenter     def as_json(*)     {     id: @data.id,     name: @data.name,     type: @data.type,     service: @data.service     }     end     end

/app/models/data.rb

    class Data     include Mongoid::Document

    field :name, type: String     field :type, type: String     field :services, type: Array

    end

This code list "first" document from Mongo Collection, and print it with Json response.

What i try to do, is list all documents from Mongo Collection, how can i do that ?

Are you unclear on what methods an Array responds to? (hint: docs)

Or is the question something else?

Hi Hassan,

can You show code example ?

Thanks

I'm not sure what you're looking for. Again, do you not know Array methods? Have you *read the doc* ?

If not, start here: Class: Array (Ruby 2.2.2)

As I wrote earlier, i try to get all documents from MongoDB collection.

In the above code example, You have:

    render json: DataPresenter.new(Data.first)

I try:

    render json: DataPresenter.new(Data.all)

But then i have exception.

Some now You know what Im try to do, maybe You can show some code example.

Thanks.

I suggest that you start by working right through a good tutorial such as railstutorial.org (which is free to use online), including doing all the exercises. That will show you the basics of Rails and Ruby. Otherwise you will later look back at this thread and be embarrassed.

Colin

Are You sure, Your tutorial shows me how to handle multiple mongo documents via presenter ?

You wanna help? Write some code example.

I know what you're trying to do; I don't know why you're having a problem with it, unless you don't know how to work with an Array.

Hence my suggestion that you read the doc, which also happens to contain "code examples" :slight_smile:

Are You sure, Your tutorial shows me how to handle multiple mongo documents via presenter ?

What you are looking for is not how to handle multiple mongo documents via presenter. What you are looking for is a basic understanding of Rails and Ruby. Without that you have no prospect of succeeding.

You wanna help? Write some code example.

You need to help yourself first. If someone supplies you with the answer to this specific question then you will soon come back with another simple problem that you should be able to solve yourself. When you have worked right through the tutorial (take several days at it, the time will not be wasted, it will repay itself very rapidly) then if you still cannot solve the problem come back again. I will be most surprised if you find that necessary though.

Colin

Thanks for speech,

Maybe someone alese.

Glad to be of help

Colin