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

ruby 1.8.6 rails 1.2.6

在视图文件/home/index.rhtml中写:

<div id="my_hint"><%= @current_hint %></div>
<%= 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


用迁移任务建好表,然后加些记录进去,就可以执行了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值