0

Group could be sorted alphabetically, but I can't rearrange the labels in alphabetical order.

Each user can post their fashion style from Fashion form(fashions/new). Fashion forms are not just underwear, there are various things such as jeans and sandals. For example, when a user selects underwear, the user selects a product name from the underwear's drop box menu classified by brand.

For now, drop box looks like

Adidas
   adidas 5
   adidas 3
   adidas 1

Nike
   Nike 3
   Nike 1
   Nike 2

want to look like

Adidas
   adidas 1
   adidas 2
   adidas 3

Nike
   Nike 1
   Nike 2
   Nike 3

There are four tables including an intermediate table.

### Fashion model
has_one :fashion_underwear
accepts_nested_attributes_for :fashion_underwear

### FashionUnderwear model(Intermediate table)
belongs_to :fashion
belongs_to :underwear

### Underwear model
has_many :fashion_underwears
belongs_to :brand

### Brand model
has_many :underwear

### Fashions.controller
def new
  @fashion = Fashion.new
  @fashion.build_fashion_underwear

  @brand = Brand.includes(:fashion_underwears).joins(:fashion_underwears).order(brand_name: :asc)
end

### Fashion/new.html
= simple_form_for(@fashion) do |f|
  = f.simple_fields_for :fashion_underwear do |p|
    = p.input :underwear_id, collection: @brand, as: :grouped_select, group_method: :underwears, group_label_method: :brand_name, label_method: :underwear_name

I studied various things and tried it, but it did not go well.

Thank you for your support!

Mike J
  • 1
  • 5
  • What does `Fashion#build_fashion_underwear` do? Please post the code from `models/fashion.rb`. Also, what version of Rails are you using? – moveson Jan 21 '18 at 02:53
  • Thank you for you support! I add details, and Rails version is 5.1.3. – Mike J Jan 21 '18 at 04:28
  • I got a hint from this article( https://stackoverflow.com/questions/20754942/grouped-collection-select-alphabetical-order-rails) and I solved it. Thank you for you support! – Mike J Jan 21 '18 at 07:19

0 Answers0