当我在Rails中手动定义选项时,如何以编程方式选择选项?

时间:2015-05-16 14:26:34

标签: ruby-on-rails-4

当然有一种聪明的方式来写这个:

  %select{:name => "status", :id => "status"}
    - if params[:status].include? "Active"
      %option Either
      %option Inactive
      %option{:selected => :selected} Active
    - elsif params[:status].include? "Inactive"
      %option Either
      %option{:selected => :selected} Inactive
      %option Active
    - else
      %option{:selected => :selected} Either
      %option Inactive
      %option Active

思想?

1 个答案:

答案 0 :(得分:0)

答案结果是:

  %select{:name => "status", :id => "status"}
    = options_for_select(["Either", "Active", "Inactive"], params[:status])