【发布时间】:2016-05-18 13:40:21
【问题描述】:
我下面的 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
【问题讨论】:
-
如果contact_title 可用,我们如何知道哪个是UI 中的默认设置值?这个任务似乎需要一些 JavaScript 代码,除非我没有理解你的意思?
-
更正了上面的小错字。