簡體   English   中英

rails 3.2 ice_cube和recurring_select

[英]rails 3.2 ice_cube and recurring_select

我正在嘗試將recurring_select保存到序列化屬性以處理Rails應用程序上的重復事件。 使用Jayson的帖子,我設法保存了時間表,但是現在我無法在索引視圖中顯示已保存的屬性,也無法在_form視圖中更新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 %>

好,我找到了! 該模型應為:

  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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM