0

我的事件模型代码是这样的:

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 eventstotal events以及如何使用更新事件计数jquery

4

1 回答 1

0

您必须:remote => true用于 AJAX 调用。

为此,您需要respond_to在控制器方法中添加一个响应 js ( format.js) 的块。有关respond_to的更多信息。

.js.erb然后,这将在您的视图中执行相应的文件。从那里您可以使用更新视图javascript(并更新事件计数)。

关于在 Rails 上使用 AJAX 的一个很好的起点。

于 2012-05-08T07:25:38.753 回答