目前我正在使用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 remote 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的名称相同,我真的很感谢你的帮助。
答案 0 :(得分:0)
将$('##{dom_id(@event)}').reset();
放入destroy.js.erb
文件。