Rails non-table drop down list

后端 未结 3 2029
余生分开走
余生分开走 2021-02-07 15:08

I need to have a drop down list in which the user selects the day of the week they want to come in every week. The values will never change right. It\'s just Sunday, Monday, ..

3条回答
  •  渐次进展
    2021-02-07 15:54

    Why create a model? Just use select.

    DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
    select(:event, :day, DAYS)
    

    It's usually better practice to place the constant in the relevant model and use it from there.

    In your model:

    class Event < ActiveRecord::Base
      DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
    end
    

    and then in your view:

    select(:event, :day, Event::DAYS)
    

    and here's another trick I use a lot:

    select(:event, :day, Event::DAYS.collect {|d| [d, Event::DAYS.index(d)]})
    

提交回复
热议问题