Rails non-table drop down list

后端 未结 3 2020
余生分开走
余生分开走 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:41

    Try this:

    <%= select_tag(:week_day, options_for_select([['Sunday', 'Sun'], ['Monday', 'Mon'], ...])) %> 
    

    See http://guides.rubyonrails.org/form_helpers.html#theselectandoptionstag

    0 讨论(0)
  • 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)]})
    
    0 讨论(0)
  • 2021-02-07 15:57

    Note that Ruby has the English names for the days of the week already built into its date class. You should try to leverage that if you can. Here's the rdoc.

    Then as Can has suggested just do the following:

    select(:event, :day, Date::DAYNAMES)
    

    Keep in mind that this solution is NOT particularly i18n friendly. If i18n is an issue, I would also check out the localized dates plugin and the changes that were made in Rails 2.2 to support i18n.

    0 讨论(0)
提交回复
热议问题