0

Is it possible to make select_tag return a pair of values (group_id, item_id) using option_groups_from_collection_for_select?

= select_tag :item_id,
          option_groups_from_collection_for_select(@groups, :items, :name, :id, :proper_name),
          class: 'form-control'

Each group has many items association, each item can belong to one or more groups.

I'd like to make the helper to add group_id to optgroup id attribute in addition to adding item_id to each item option and fetch those two on selection to send them to a controller, but I can't figure out how, nor is it even possible (or proper..).

I could make two different dropdowns, where 2. one would be loaded using ajax after selecting an option in the 1. one, but this just doesn't seem right..

EDIT

I've figured that I can pass a proc as a value_method, but unfortunately I'm unable to fetch group_id in there..

= select_tag :item_id,
          option_groups_from_collection_for_select(@groups, :items, :name, ->(el) { "#{el.id}" }, :proper_name),
          class: 'form-control'

Kind regards!

EDIT

Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)

Chaos
  • 1
  • 1

1 Answers1

0

Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)

Used grouped_options_for_select and passed group_id as a data attribute to each option, which I'd pass along with url later using simple JS.

Chaos
  • 1
  • 1