0

目前我正在使用 rails 3.0.3 开发一个网站。

我有一个名为events的模型。在我的events_controller.rb文件中,我有这样的代码:

def destroy
  @event = Event.find(params[:id])
  @event.destroy
  #redirect_to events_url
  respond_to do |format|
    format.html { redirect_to (events_url), :notice => 'Event deleted' }
    format.js
  end
end

我有一个index.html.haml文件,我写了这个:

- content_for :title do
  Listing Events

%div#event
  = render :partial => "task"

我有一个名为_task.html.haml的部分文件 ,我在其中分配了 div id 和 ajax 远程 true:像这样-

- @events.each do |event|
  %ol.hoverbox
    %li.all{:id =>"event_#{event.id}"}
      = link_to image_tag(event.photo.url), event_path(event)
      .abc
        = event.name
        %br/
      .bca
        = event.start_date
        = event.start_time
        = link_to 'Delete', event, :confirm => 'Are you sure?',
                                   :method => :delete,
                                   :remote => :true

最后我有我 写的destroy.js.haml -

$('##{dom_id(@event)}').remove();

它使用 ajax 成功删除,但计数器显示相同的值,手动刷新页面后计数器正在减少。请告诉如何通过 ajax 更新。

在我的模型event.rb文件中,我正在计算这样的事件数以维护计数器:

   def current_date
        Date.today
    end

    def self.find_current
        Event.find :all, :conditions => ['(start_date  = current_date)'], :order => 'start_date '
    end

    def self.count_past(current_date = Date.today)
       Event.count :all, :conditions => ['start_date < ?', current_date], :order => 'start_date'
    end

    def self.count_upcoming(current_date = Date.today)
       Event.count :all, :conditions => ['start_date > ?', current_date], :order => 'start_date'
   end

    def self.count_today(current_date = Date.today)
        Event.count :all, :conditions => ['start_date = ?', current_date], :order => 'start_date'
    end

我还有一个部分位于名为subnav.html.haml的布局中,我在网页上显示计数

      %li
        - if params[:controller] == "brands"
          // my code
        - elsif params[:controller] == "events"
          %a.blue.tt-top-center{:title => "Today's Events", :href => events_path(:view => "today") }#{Event.count_today}
          %span Today's Events

  %li
    - if params[:controller] == "brands"
 //my code
    - elsif params[:controller] == "events"
      /= link_to "Upcomming events", events_path(:view => "upcoming") 
      %a.tt-top-center{:title => "upcomming Events", :href => events_path(:view => "upcoming") }#{Event.count_upcoming}
      %span Upcoming Events

我搜索了许多链接,但没有更新我的计数的方法,请帮助我并以详尽的方式让我理解,因为在网站上他们给出的提示与我的标准不匹配,所以请帮助我并使用相同的名称我在我的代码中使用的类和 url,我非常感谢你的帮助。

4

1 回答 1

0

入档$('##{dom_id(@event)}').reset();destroy.js.erb

于 2012-05-11T13:20:43.443 回答