Ruby On Rails

ROR API 接收 IOS(Iphone/IPAD) post 的图片 (base 64 解决)

IOS 用的是 ASIFormDataRequest

ROR接收的数据像十六进制的东西 如图

流的格式 imagedata bytes 如图:

ror怎么直接使用这个值还真不清楚  单把 二/八/十六 进制的东西转换为图片倒是简单

那么这里隆重推荐 base64 

采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。

我们网页form提交一个图片 设置类型为 multipart/form-data 我们在后台可以直接得到一个data对象然后上传到服务器

代码示例(直接保存文件):

#file为上传的data
File.open("#{RAILS_ROOT}/public/route_images/#{@filename}", "wb") do |f|
        f.write(file.read)
end

那么接收base64位数据ror也有很好的处理

#file为base64数据
path= RAILS_ROOT+"/public/route_images/#{@filename}"
image = Base64::decode64(file)
File.open(path, "wb" ) do |back_out|
     back_out      

ROR mysql 5.1.x 在ubuntu 10.04 The bundled mysql.rb driver has been removed from Rails 2.2

ErrorMessage:

The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.

sudo apt-get install libmysql-ruby

错误详情:

#此时报错 下面解决
ray@ubuntu:~/appfood$ ruby script/server
=> Booting WEBrick
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-09-01 21:46:35] INFO  WEBrick 1.3.1 更多 >

ROR 分页will_paginate 使用

安装插件will_paginate:

Microsoft Windows [版本 5.2.3790]
(C) 版权所有 1985-2003 Microsoft Corp.

C:\Documents and Settings\Administrator>gem install mislav-will_paginate --sourc
e http://gems.github.com
Fetching: mislav-will_paginate-2.3.11.gem (100%)
Successfully installed mislav-will_paginate-2.3.11
1 gem installed
Installing ri documentation for mislav-will_paginate-2.3.11...
Installing RDoc documentation for mislav-will_paginate-2.3.11...

配置environment.rb:

require 'will_paginate'

控制器使用:

@markets = Market.find_by_key(params[:key]).paginate(:page => params[:page]||1, :per_page => 32)

页面显示:

will_paginate @markets, {:class=>'scott', :prev_label   => '上一页',:next_label   => '下一页'} 更多 >

ROR APNS(Apple Push Notification Service ) DEMO

找的了个gem代码 看的眼花 有没有搞错啊 那么复杂 http://apnonrails.metabates.com/ 鄙视之 来个终极代码:

def apn
#首先找到你的pem文件 里面一大堆授权码 自己去转换  我这里把它放到了项目的config文件夹下面 名字叫DEV21.pem
	cert = File.read(File.join(RAILS_ROOT, 'config', 'DEV21.pem'))
	ctx = OpenSSL::SSL::SSLContext.new
	ctx.key = OpenSSL::PKey::RSA.new(cert, '')
	ctx.cert = OpenSSL::X509::Certificate.new(cert)

	sock = TCPSocket.new('gateway.sandbox.push.apple.com', 2195) #development gateway
	ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
	ssl.connect

	payload = {"aps" => {"alert" => "Oh hai!", "badge" => 1, "sound" => 'default'}}
	json = payload.to_json() 更多 >

ROR RSS ADD content IMAGE

#添加引用 很重要
require 'rss/maker'

def newsrss
    @news = News.all()
    feed = RSS::Maker.make("2.0") do |maker|
      maker.channel.title = "Market Title"
      maker.channel.description = "Market Des"
      maker.channel.link = "http://www.camxi.com"
      maker.image.url = "http://hiiii.vicp.net:3000/images/logo.gif"
      maker.items.do_sort = true
      if @news && @news.size > 0
        @news.each do |news|
          item = maker.items.new_item
          item.link = "http://hiiii.vicp.net:3000/news/#{news.id}"
          item.title = news.name
          #item.image 根本没有也就不打算颜色maker的代码了 更多 >

Win7 快速搭建开发Ruby On Rails(ROR) 环境

用到的客户端软件有:

1,rubyinstaller-1.8.7-p334

2,rubygems-1.4.2

3,mysql-5.0.19

这些都可以到http://rubyforge.orgqu 去下载

废话少说10分钟应该可以收工 按我命令提示行来

 

安装ruby

 

安然rubygems

 

安装 rails 在安装mysql就可以 启动服务了

 

 

打开浏览器 收工

 

IDE的话Netbeans 6.9.1是可以开发ROR的,但是7.0以上就不行了

 

 

在window server 2003 安装rails 的时候老是找不到包:

gem update –system

但是安装好rails的后执行 rails test的时候报错

回到gem 1.4.2的文件夹 执行

ruby setup.rb

覆盖最新的rubygem版本就ok

 

 

 

这里贴下代码 有空看看就明白了

C:\Documents and Settings\Administrator>gem install rails -v=2.3.8
WARNING:  RubyGems 1.2+ index not found for:

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 1 gems from http://rubygems.org/
.
complete
Updating 更多 >

Ubuntu 10.04 配置ROR 环境

To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.

aiice@aiice-desktop:~$ sudo lsb_release -a
[sudo] password for aiice:
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 10.04.2 LTS
Release:	10.04
Codename:	lucid

aiice@aiice-desktop:~$ sudo apt-get install ruby-full
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成 更多 >

ROR 文字截取问题解决

假如 一个DIV的宽带可以放100个汉字,能放200个字母,如果普通的截取便显示不美观 长的长短的短 那么用我的找个方法试试看


#ruby代码开始
#自定义长度和...符号
def truncate_text(text, length = 10, t_string = '...')
    return "" if !text
    l=0
    char_array=text.unpack('U*')
    char_array.each_with_index do |c,i|
      l = l+ (c<127 ? 0.5 : 1)
      if l>=length
        return char_array[0..i].pack('U*')+(i     

ROR 文字时间方法application_helper

经常看到某文字 发表时间是 :20分钟前, 有时候是几小时前 那么久实现这个效果吧

def distance_of_time_in_words(from_time, to_time = 0, include_hms = true)
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round

    case distance_in_minutes
    when 0..1           then (distance_in_minutes==0) ? '刚刚' : '1分钟前'
    when 2..59          then "#{distance_in_minutes}分钟前"
    when 60..90         then "1小时前"
    when 90..1440       then "#{(distance_in_minutes.to_f / 更多 >

ROR简单集成支付宝

两年前的代码  有点笨拙 但是也还能用 由于支付宝API丢了 也不知道最新规定是什么 但是这样试够用了的

protect_from_forgery :except => [:alipay_notify] #这个别忘记了

#支付页面 连接支付网关
  def pay_prepay
    @order = OrderEvent.find(params[:id])
    @user = current_user
    @alipay = @user.alipay
    @a = "service=create_partner_trade_by_buyer"
    @b = "partner=2088***"
    @d = "subject=#{@order.event_id}"
    @e = "out_trade_no=#{@order.id}"
    @f = "price=#{@order.event_prepay/100}"
    @g = "show_url=http://www.aiice.com" #只是比方 这里是商品show页面
    @h = "notify_url=http://www.aiice.com/alipay_notify" #这个是支付宝通知你的页面
    @i = "quantity=1" 更多 >