#10 楼 @phperyang require 'hpricot doc = Hpricot::XML(xml) (doc/:channel/:item).each do | url | title = Iconv.conv("utf8","GBK",(url/:title).text) link = Iconv.conv("utf8","GBK",(url/:link).text) newlink = "http://74.82.175.214:4568/jump?url=#link}{" pubDate = Iconv.conv("utf8","GBK",(url/:pubDate).text) source = Iconv.conv("utf8","GBK",(url/:source).text) author = Iconv.conv("utf8","GBK",(url/:author).text) strs = "[#{v}]标题:#{title} 发布时间:#{pubDate} 来源:#{source} " @yang.unshift(strs) end
读取的 XML
很简单了。。。 就是一个 model 数据层文件夹 views 模板文件夹 index.rb 主页
index.rb: #Ruby Sinatra #author [email protected] #gem install will_paginate =begin ./hi.rb Controller ./model/y.rb Model ./views/index.erb Views =end
require 'sinatra' require 'will_paginate' require 'will_paginate/active_record' require File.join(File.dirname(FILE),'model/y.rb') #数据层 #首页 get '/' do erb :index end #各类别的 get '/imop' do @key = params['name'] unless @key.blank? #"Hello #{params['name']}!" #params['username'] params[:username] @p = Yang.where("t=?", @key).paginate(:page => params[:page],:per_page => 20).order('id DESC') #@p = Yang.find_by_sql("select * from yangs where type='#{@key}'") end erb :imop end
post '/add' do @title = params['title'] @t = params['t'] @author = params['author'] @url = params['url'] ttt = @title.empty? uuu = @url.empty? if ttt != true res = Yang.create(:title => @title ,:t => @t ,:author => @author,:url => @url) if res "#{@title} #{@t} #{@authore} #{@url}" redirect back end else "资料不能为空哈." end
end
views: index.erb: 就是个 form 表单
#5 楼 @alucardpj 嗯。VPS 在国外。。。
#2 楼 @fresh_fish 可以了。。程序里面端口错了
目前是在我的 VPS 上直接 ruby -rubygems index.rb -p 4568 & 直接启动的。。可以安装一下 thin 服务器