All threads / grouped_options_for_select --- include a select all
Ask A Question

Notifications

You’re not receiving notifications from this thread.

grouped_options_for_select --- include a select all

Amanda asked in Rails

Hi,

I have Categories and CategoryType. A Category has_many CategoryTypes. In my form, I am using grouped_options_for_select to display the different category types for a category.

Form:

 <%= f.select :category_type_id,  grouped_options_for_select(@grouped_category_type_options),  {'data-width' => "100%", include_blank: 'Categories'}, {:class => "form-control border-0 shadow-0 transparent-form"} %>

Being set in the controller:

  @grouped_category_type_options = CategoryType.includes(:category).inject({}) do |options, type|
    (options[type.category.name] ||= []) << [type.name, type.id]
    options

What I want to know is that is there a way for me to also include an "all" option for the user to select "all" the category types for a set category. For example, for the category of "Hotels," the category types are "Hostel", "B&B", and "Motel". I want to also include an "All" option that will allow me to filter for all hotels regardless of the set category type.

Can anyone help me with this or point me in the right direction?

Thanks!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 66,029+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.