我的活动模型代码是这样的:
def current_date
Date.today
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'
#this statment is equivalent to
#select * from events where start_date > current_date order by start_date
end
def self.count_today(current_date = Date.today)
Event.count :all, :conditions => ['start_date = ?', current_date], :order => 'start_date'
end
我的view(index file)
代码是这样的:
%li
%a.tt-top-center{:title => "Total Events", :href => events_path}#{Event.all.size}
%span Total events
%li
%a.blue.tt-top-center{:title => "Today's Events", :href => events_path(:view => "today")}#{Event.count_today}
%span Today's Events
%li
%a.green.tt-top-center{:title => "Past Events", :href => events_path(:view => "past")}#{Event.count_past}
%span Past Events
%li
%a.tt-top-center{:title => "Upcoming Events", :href => events_path(:view => "upcoming")}#{Event.count_upcoming}
%span Upcoming Events
我的controllers
代码是这样的:
def index
case params[:view]
when 'past'
@events = Event.find (:all, :conditions => ['start_date < ?', current_date], :order => 'start_date')
when 'today'
@events = Event.find (:all, :conditions => ['(start_date = current_date)'], :order => 'start_date ')
when 'upcoming'
@events = Event.find(:all, :conditions => ['start_date > ?', current_date], :order => 'start_date')
else
@events = Event.all
end
end
请点击各个链接ajax
,today's events
等,告诉我如何使用total events
?如何使用jquery
<更新事件计数? / p>
答案 0 :(得分:0)
您必须使用:remote => true
进行AJAX调用。
为此,您需要在控制器方法中添加一个响应js(respond_to
)的format.js
块。有关respond_to的更多信息。
然后,这将在您的视图中执行相应的.js.erb
文件。从那里,您可以使用javascript
更新视图(并更新事件计数)。
使用AJAX on Rails的一个很好的起点。