我的haml代码应该显示一个包含三个不同选项(QA,Staging,Production)的下拉表单。如果contact_title
没有值或等于nil
,则它在SQLite DB中设置为QA的默认值。如果设置了它应该在UI中显示默认设置值。当前代码不起作用。最优雅的方式是什么?
%tr
%td{:style => 'width: 30%'}
QA or Production
%td{:style => 'width: 70%'}
%label{:type => 'text', :name => 'contact_title', :value => "#{@report.contact_title}"}
.controls
%select{:name => "contact_title"}
%option{:selected => "selected" } QA
%option{:select => "select" } Staging
%option{:select => "select" } Production
- if @report.contact_title.nil? or @report.contact_title == 0
%option{:selected => "selected" } QA
%option Staging
%option Production
- if @report.contact_title == 'Staging'
%option{:selected => "selected" } Staging
%option QA
%option Production
- if @report.contact_title == 'Production'
%option{:selected => "selected" } Production
%option QA
%option Staging
答案 0 :(得分:0)
将模型添加到select_tag。
%tr
%td{:style => 'width: 30%'}
QA or Production
%td{:style => 'width: 70%'}
%label{:type => 'text', :name => 'contact_title', :value => "#{@report.contact_title}"}
.controls
=select_tag "contact[contact_title]", options_for_select(['QA', 'Staging', 'Production'])