【问题标题】:Drop down form not saving to database下拉表单不保存到数据库
【发布时间】: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 代码,除非我没有理解你的意思?
  • 更正了上面的小错字。

标签: ruby sinatra haml


【解决方案1】:

将模型添加到 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'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    相关资源
    最近更新 更多