ruby 常用函数

 
def status_time_ago_in_words(time)   
 time = time_ago_in_words(time)   
 case time   
 when "less than a minute"  
   "刚刚"  
 when /minute/   
    time.gsub(/minute|minutes/,'分钟') + "前"  
 when /hour/   
    ##time.gsub(/about/,'').gsub(/hours/,'小时') + "前"   
    time.gsub(/about (\d+) (hour|hours)/, '\1小时') + "前后"  
 when "1 day"  
    "昨天"  
 when "2 days"  
    "前天"  
 else  
   time.gsub(/days/,'天') + "前"  
 end  
nd 
 wer
    case request.method
    when :post
       Emailer.deliver_contact('vwangzhen@126.com', 'Please active 

your Account','send message','http://www.baidu.com')
    end

 

字符集转换
     require 'iconv'
 conv = Iconv.new("gbk", "utf-8")
    puts conv.iconv(q)
 让页面延迟两秒发送,目地让数据加载完毕
    # render as finished
    responds_to_parent do # execute the redirect in the main window
      render :update do |page|
        page.call "UploadProgress.setAsFinished"
        page.delay(2) do # allow the progress bar fade to complete
          page.redirect_to url_for(:action => "complete")
        end
      end  
    end
  ruby 的case 方法与request 结合
    case request.method
    when :post
       Emailer.deliver_contact('vwangzhen@126.com', 'Please active 

your Account','send message','http://www.baidu.com')
    end
  

 

ruby在用当前action执行Ajx请求
 
        respond_to do |format|
          format.html # index.html.erb
         format.xml  { render :xml => @contracts }
          format.js { 
            render :update do |page|
              page.replace("list" , :partial => "list" )
        end       
          }
        end

 

  evel函数
          eval("
 page.replace('image#{picture_position}',image_tag('/upload/'+'#

{name}',:id=>'image#{picture_position}',:width=>180,:height=>130))
  page.replace('product_image#{picture_position}',hidden_field

(:product, :image#{picture_position},:value=>'/upload/#{name}'))  
            ")
     page.call("top_alert","#{:image_null_input_error.l()}") 
 
 
var x = document.getElementById('abc');

name = x.childNodes[x.childNodes.length-1].name;

setTimeout('myol()',4000);
function myol()
{
	var mytag = window.frames[name].document.getElementsByTagName

('a');
	alert(mytag[0].href);
}
 
        <%=  select(:contract, :department_id, @depart ,{}, 
          { "onchange" => remote_function(
              :with => "'parent_id='+value", 
              # :update => 'county_select', 
              #:complete => "Element.show('county_select')", 
              :url => { :action => :select_with_ajax1 } ) })%> 
 
    <%=option_groups_from_collection_for_select(@allgroups, :members, 

:name, :full_name, :full_name,s @selected_manager) %>
 
   取随机数
    chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789' 

 
    randchar = ''  
    length.downto(1) { |i| randchar<< chars[rand(chars.length - 1)] }  

  
 
 def self.generate_randchar(length=6)   
    chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789' 

 
    randchar = ''  
    length.downto(1) { |i| randchar<< chars[rand(chars.length - 1)] }  

   
    randchar
  end
    
  def self.base_part_of(file_name)
    File.basename(file_name).gsub(/[^\w._-]/, '')
  end

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值