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 => trueAJAX呼び出しに使用する必要があります。

respond_toこのためには、js()に応答するコントローラーメソッドにブロックを追加する必要がありますformat.jsreply_toに関する詳細情報。

これにより.js.erb、ビュー内の対応するファイルが実行されます。そこから、を使用してビューを更新できますjavascript(およびイベントカウントを更新できます)。

RailsでAJAXを使用するための良い出発点。

于 2012-05-08T07:25:38.753 に答える