2013-11-04 62 views
0

我有一組7個下拉輸入,允許管理員在給定日期內說明它們是打開還是關閉。我想變成7開/關開關(大概是風格複選框?),但無法弄清楚如何做到這一點!將選擇輸入更改爲複選框,用作Rails中的開/關切換開​​關

下面是代碼中的相關位我現在有(之前的任何變化):

應用程序/視圖/後端/ 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") 

應用程序/車型/ 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 

而且在應用程序/控制器/ BAC DMOZ目錄/ 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 

回答

1

這可能是一個相當沉重的變化,但我發現這樣做是非常相似的Ryan Bates' Railscast on using a bitmask的最佳途徑。本教程非常棒,設置起來相對較快,並且應該很容易地從用戶角色(本教程中使用的內容)映射到應用中的Workday一週中的某幾天。

+0

謝謝。這很有用,因爲我是Ruby和Rails的新手,並且不熟悉check_bog_tag語法。我想我現在已經完成了。 – Ribena

+0

太棒了!如果這個答案有幫助,請給這個答案一個複選標記以關閉問題! :) – CDub

0

好的。在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