2014-10-07 69 views
1

我試圖將recurring_select保存爲序列化屬性以處理rails應用程序上的重複事件。 使用Jayson's post我設法得到保存的時間表,但現在我無法得到保存 屬性索引視圖中顯示或更新_form鑑於recurring_selectrails 3.2 ice_cube和recurring_select

這是我的模型

class Todo < ActiveRecord::Base 

    attr_accessible :item, :completed, :schedule, :start_date 
    after_initialize :default_values 

    validates :item, presence: true 
    belongs_to :list 

    belongs_to :tasklib, 
      :foreign_key=>"item" 

    #recuring model 
    include ActiveModel::Validations 
    include ActiveModel::Conversion 
    extend ActiveModel::Naming 

    attr_accessor :schedule 

    serialize :schedule, Hash 

    def schedule=(new_schedule) 
    write_attribute(:schedule,RecurringSelect.dirty_hash_to_rule(new_schedule).to_hash) 
    end 

    def converted_schedule 
    the_schedule = Schedule.new(self.start_date) 
    the_schedule.add_recurrence_rule(RecurringSelect.dirty_hash_to_rule(self.schedule)) 
    the_schedule 
    end 

end 

這是我的索引視圖:

h1><%= @list.name %></h1> 
<table class="table table-striped"> 
    <thead> 
    <tr>  
     <th><%= model_class.human_attribute_name(:item) %></th> 
     <th><%= model_class.human_attribute_name(:start_date) %></th>  
     <th><%= model_class.human_attribute_name(:schedule) %></th>  
     <th><%=t '.actions', :default => t("helpers.actions") %></th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @list.todos.each do |todo| %> 
     <tr>   
     <td><%= todo.item %></td> 
     <td><%= todo.start_date %></td>   
     <td><%= todo.schedule %></td> 

     <td> 
      <%= link_to t('.edit', :default => t("helpers.links.edit")), 
         edit_list_todo_path(@list, todo), :class => 'btn btn-mini' %> 
      <%= link_to t('.destroy', :default => t("helpers.links.destroy")), 
         list_todo_path(@list, todo), 
         :method => :delete, 
         :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')), 
         :class => 'btn btn-mini btn-danger' %> 
     </td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

,這是我的看法_form:

<%= simple_form_for [@list, if @todo.nil? then @list.todos.build else @todo end], :html => { :class => 'form-horizontal' } do |f| %> 

    <%-# f.input :item, input_html: {class: "span6", rows: 3} -%> 

    <%= f.collection_select :item, Tasklib.order(:name),:name,:name, include_blank: true %> 

    <%= f.label :start_date, "date" %> 
    <%= f.input :start_date %> 

    <%= f.label :schedule %> 
    <%= f.select_recurring :schedule, nil, :allow_blank => true %> 

    <div class="form-actions"> 
    <%= f.submit 'Save', :class => 'btn btn-primary' %> 
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")), 
       lists_path, :class => 'btn' %> 
    </div> 
<% end %> 

回答

2

好的,我發現它! 的模式應該是:

def schedule=(new_schedule) 
    if new_schedule == nil 
     new_schedule = IceCube::Schedule.new(self.start_date) 
    end 

    write_attribute(:schedule, RecurringSelect.dirty_hash_to_rule(new_schedule).to_hash) 
    end 


    def converted_schedule 
    if !self.read_attribute(:schedule).empty? 
    the_schedule = IceCube::Schedule.new(self.start_date) 
    the_rule = RecurringSelect.dirty_hash_to_rule(self.read_attribute(:schedule)) 
    if RecurringSelect.is_valid_rule?(the_rule) 
     the_schedule.add_recurrence_rule(the_rule) 
    end 
    the_schedule 
    end 

    end 

和表單視圖應該只設置一個新的定期選擇,如:

<%= f.select_recurring :schedule, [ ["No recurring", "{}"] ], :allow_blank => true %>