user2631108
user2631108

Reputation:

How return all elements array in Rails?

I have a controller:

def grafico_gantt 
    @mapa = Hash.new
    @mapa[:tasks] = []
    @projeto.atividades.each do |a|
        @mapa[:tasks] << {
           id:a.id,
           descricao:a.descricao,
           status:a.status,
           data_inicial:a.data_inicial.to_datetime.to_i*1000,
           tempo_gasto:a.tempo_gasto.to_i,
           data_final:a.data_final.to_datetime.to_i*1000
        }
    end
end

And a .js.erb

<script>
    $(function() {
        "use strict";
        $(".gantt").gantt({
            source: [{
                name: '<%= raw @mapa[:tasks][0][:descricao] %>',
                desc: '<%= raw @mapa[:tasks][0][:status] %>'+"% concluído",
                values: [{
                    from: "/Date(<%= raw @mapa[:tasks][0][:data_inicial] %>)/",
                    to: "/Date(<%= raw @mapa[:tasks][0][:data_final] %>)/",
                    label:"<%= raw @mapa[:tasks][0][:descricao] %>", 
                    customClass: "ganttRed"
                }]
            }],
        scale: "days",
        minScale: "days",
        maxScale:"months",
        navigate: "scroll",
        waitText: "Aguarde...",
    });
</script>

I have 3 values in arrays, but, how can see, i just take 1, @mapa[:tasks][0][:descricao] How a can take all values? Because @mapa[:tasks][:descricao] don't work :/

Thanks!

Upvotes: 1

Views: 1888

Answers (2)

the Tin Man
the Tin Man

Reputation: 160551

First, I'd write the controller more like this (untested) code:

def grafico_gantt 
  @mapa = {}
  @mapa[:tasks] = @projeto.atividades.map { |a|
    {
      id: a.id,
      descricao: a.descricao,
      status: a.status,
      data_inicial: a.data_inicial.to_datetime.to_i * 1000,
      tempo_gasto: a.tempo_gasto.to_i,
      data_final: a.data_final.to_datetime.to_i * 1000
    }
  }
end

To access all the descricao elements of the returned array, use a map or collect to transform the array into just the parts you want:

@mapa[:tasks].collect { |e| e[:descricao] }

map and collect are synonymous. Though they are aliased, sometimes it makes more sense syntactically to use collect, and sometimes map makes more sense.

In the rewrite of the code above, it's a little confusing why you create a hash of hashes. A single element hash of hashes is a bit... confusing... inelegant, but maybe you're not showing us everything.

If nothing else is added to the hash, I'd recommend simplifying it to:

def grafico_gantt 
  @mapa = @projeto.atividades.map { |a|
    {
      id: a.id,
      descricao: a.descricao,
      status: a.status,
      data_inicial: a.data_inicial.to_datetime.to_i * 1000,
      tempo_gasto: a.tempo_gasto.to_i,
      data_final: a.data_final.to_datetime.to_i * 1000
    }
  }
end

Which will return an array of hashes and simplify your accesses into it by removing the need to find the [:tasks] element:

@mapa.collect { |e| e[:descricao] }

Upvotes: 2

Arup Rakshit
Arup Rakshit

Reputation: 118271

how is this :

@mapa[:tasks].each{|e| puts e[:descricao] }

or

@mapa[:tasks].map{|e| e[:descricao] }

Upvotes: 0

Related Questions