利用数组和ajax实现单条翻动标题新闻

阅读更多
ruby 1.8.6 rails 1.2.6

在视图文件/home/index.rhtml中写:
<%= @current_hint %>
<%= periodically_call_remote :url => {:action => "get_next_hint"},:frequency=>2 %>


在控制器文件home_controller.rb中写:
class HomeController < ApplicationController
  def index
    session[:hint]= HintNew.find(:all,:select=>"title")
    @current_hint = session[:hint].first.title
  end
  
  def get_next_hint
   if session[:hint].size > 0  
      session[:hint].shift
   else
      session[:hint]= HintNew.find(:all,:select=>"title")        
   end
   @current_hint = session[:hint].first.title
  end
end


在视图中建立文件:get_next_hint.rjs,内容如下:
page['my_hint'].replace_html @current_hint


其中,HintNew模型对应表:hint_news
迁移任务文件:005_create_hint_news.rb定义如下:

class CreateHintNews < ActiveRecord::Migration
  def self.up
    create_table :hint_news do |t|
      t.column :title,:string
      t.column :body,:text
    end
  end

  def self.down
    drop_table :hint_news
  end
end


用迁移任务建好表,然后加些记录进去,就可以执行了。

你可能感兴趣的:(Ajax,Ruby,Rails,ActiveRecord,UP)