jeudi 28 janvier 2021

Multiple select with custom data

I have a form with a multiple select like this: 

.form-row__select
        = f.collection_check_boxes(:game_ids, @games, :id, :name)

The collection @games worked because the data was returned this way:

=> #<Game:0x00007fa12c3a9560
 id: 5,
 name: :example,
 active: true,
 ...

But now I need to add custom data to my collection and I was not able to make the multiple select work:

game = Game.enabled.includes(:category)
    @games = game.map do |s|
       [[s.category.name, I18n.t(s.type_code, scope: 'enum.type.name')].join(' - '), s.id]
    end
@games.sort!

The result of the above method is an array of array like this:

[["Example one - Category X", 5],
["Example four - Category Y", 1]]
...

How can I make a multiple select using the array above and showing its strings so they can be selected? Thank you so much for your help!




Aucun commentaire:

Enregistrer un commentaire