【问题标题】:Changing a select input to a checkbox acting as an on/off toggle switch in Rails将选择输入更改为在 Rails 中充当开/关切换开​​关的复选框
【发布时间】:2013-11-04 03:51:14
【问题描述】:

我有一组 7 个下拉输入,允许管理员说出他们在给定日期是营业还是停业。我希望将其更改为 7 个打开/关闭开关(大概是样式复选框?)但不知道该怎么做!

以下是我目前拥有的相关代码(在任何更改之前):

app/view/backend/inventory_pool/edit.html.haml

- content_for :title, @inventory_pool
  = form_for [:backend, @inventory_pool], html: {name: "form"} do |f|

    .content
      - if is_admin?
        %a.button{:href => root_path}= _("Cancel")
      %button.button{:type => :submit}= _("Save %s") % _("Inventory Pool")

      %section
        %h2= _("Basic Information")
        .inner
          .field.text
            .key
              %h3= "#{_("Print Contracts")}"
              %p.description
            .value
              .input
                %input{type: "checkbox", name: "inventory_pool[print_contracts]", checked: @inventory_pool.print_contracts}


      %section#workdays
        %h2= _("Workdays")
        .inner
          - [1,2,3,4,5,6,0].each do |i|
            .field.text
              .key
                %h3= "#{I18n.t('date.day_names')[i]}"
              .value
                .input
                  %select{:name => "store[workday_attributes][workdays][]"}
                    %option{:label => _("Open"), :value => Workday::WORKDAYS[i]}= _("Open")
                    %option{:label => _("Closed"), :value => "", :selected => @store.workday.closed_days.include?(i) ? true : nil}= _("Closed")

app/models/workday.rb

class Workday < ActiveRecord::Base

  belongs_to :inventory_pool

  WORKDAYS = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]

  def is_open_on?(date)

    return false if date.nil?

    case date.wday
    when 1 return monday
    when 2 return tuesday
    when 3 return wednesday
    when 4 return thursday
    when 5 return friday
    when 6 return saturday
    when 0 return sunday
    else
      return false #Should not be reached
    end
  end

  def closed_days
    days = []
    days << 0 unless sunday
    days << 1 unless monday
    days << 2 unless tuesday
    days << 3 unless wednesday
    days << 4 unless thursday
    days << 5 unless friday
    days << 6 unless saturday
    days
  end

  def workdays=(wdays)
    WORKDAYS.each {|workday| write_attribute(workday, wdays.include?(workday) ? true : false)}
  end
end

app/controllers/backend/inventory_pools_controller 我有这个(删节):

  def update
    @inventory_pool ||= InventoryPool.find(params[:id])
    process_params params[:inventory_pool]
  end

  def process_params ip
    ip[:print_contracts] ||= "false" # unchecked checkboxes are *not* being sent
    ip[:workday_attributes][:workdays].delete "" if ip[:workday_attributes]
  end

【问题讨论】:

    标签: ruby-on-rails forms haml


    【解决方案1】:

    这可能是一个相当大的变化,但我发现最好的方法与Ryan Bates' Railscast on using a bitmask 非常相似。本教程非常棒,设置起来相对较快,并且应该很容易地从用户角色(在教程中使用)映射到您应用中 Workday 上的一周中的几天。

    【讨论】:

    • 谢谢。这很有用,因为我是 Ruby 和 Rails 的新手,并且不熟悉 check_bog_tag 语法。我想我现在已经解决了。
    • 太棒了!如果此答案有帮助,请通过勾选此答案来关闭问题! :)
    【解决方案2】:

    好的。在haml中,我不得不替换:

                .input
                  %select{:name => "store[workday_attributes][workdays][]"}
                    %option{:label => _("Open"), :value => Workday::WORKDAYS[i]}= _("Open")
                    %option{:label => _("Closed"), :value => "", :selected => @store.workday.closed_days.include?(i) ? true : nil}= _("Closed")
    

    与:

            = check_box_tag "inventory_pool[workday_attributes][workdays][]", Workday::WORKDAYS[i], @inventory_pool.workday.closed_days.include?(i) ? nil : true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-01
      • 2013-09-23
      • 2020-02-05
      • 2018-02-15
      • 2021-09-09
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多