Ruby On Rails开发技巧总结(不断更新,备忘)

Ruby On Rails开发时的技术还是很多很杂的,我现在把它们整理,列出来,方便自己,也方便大家。我的开发网站:[url][/url]

注:我在其中所列的,也有不是ROR专有的,但是在Rails开发中实用。

A:View
1。Helper number_to_currency(@book.price)格式化显示时间,$89.00,合适开发英文网站。

2。图片按钮提交表单,
<input type='image' src='/images/button2.gif' style='width:120px;height:30px;'/>
把它放在<form>内,和submit button的作用一样,另说明一下,这个网站上可以在线为我们生成自行设置的图片,很方便,[url]http://www.buttonator.com/[/url],我又看了一下这个网站,感觉它也很优秀,[url]http://www.mycoolbutton.com[/url]

3。奇偶行变色:
<%=cycle('list-line-odd', 'list-line-even') %>


4。鼠标放上去变色,这个虽然是HTML方面的代码,但是在WEB开发中也用得比较多,所以记一下。
onmouseover="this.style.background='#FFFCDF';" onmouseout="this.style.background='#FFF';"


5。改进Flash消息的显示。Flash消息显示4秒后,淡出。

<% if flash[:warning] or flash[:notice] %>
<div id="flash_message" <% if flash[:warning] %>class="warning"<% elsif flash[:notice] %>class='notice'<% end %> >
<%= flash[:warning] || flash[:notice] %>
</div>
<script type="text/javascript">
setTimeout("new Effect.Fade('flash_message');", 4000)
</script>
<% end %>

别忘记了<%= javascript_include_tag :defaults %>,另外,对于Flash消息显示的css代码如下,
.notice{
margin-top:5px;
padding: 8px;
border-top:2px solid #73E673;
border-bottom:2px solid #73E673;
background:#B6F2B6;
}
.warning{
margin-top:5px;
padding: 8px;
border-top:2px solid #FFF280;
border-bottom:2px solid #FFF280;
background:#FFF9BF;
}


6。为了避免过长字符串撑开页面,经常需要调用截取过长字符串的方法,rails已经为我们提供了一个方法:
ActionView::Helpers::TextHelper#truncate(text, length = 30, truncate_string = "...") 

中英文混合字符串截取,见:http://www.iteye.com/topic/201531

#Quake Wang的做法
def truncate_u(text, length = 30, truncate_string = "...")
if r = Regexp.new("(?:(?:[^\xe0-\xef\x80-\xbf]{1,2})|(?:[\xe0-\xef][\x80-\xbf][\x80-\xbf])){#{length}}", true, 'n').match(text)
r[0].length < text.length ? r[0] + truncate_string : r[0]
else
text
end
end


#庄表伟的做法
def truncate_u2(text, length = 30, truncate_string = "...")
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<char_array.length-1 ? truncate_string : "")
end
end
return text
end


7。图形验证码的使用,首先要装上rmagick,将附件里的rb文件放在/app/models里,图片的引用src='xxx/code_image',则在xxx控制器的code_image方法定义如下[img]http://www.iteye.com/upload/attachment/32304/784b9247-d1a2-37ae-b0a1-f60f9855e10b.jpg[/img]:

def code_image
session[:noisy_image]=NoisyImage.new(4)
session[:code] =session[:noisy_image].code
image = session[:noisy_image].code_image
send_data image, :type => 'image/jpeg', :disposition => 'inline'
end

相信你看了以上的代码,也就知道怎么样验证用户输入的验证码是否一致了吧。

8。JavaEye的时间格式显示
# 将此方法放在/app/helpers/中

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
end


9。格式化数字以千分位形式显示
ails的ViewHelper中有[color=red]number_with_delimiter[/color](number, delimiter=",", separator=".")方法,它的源代码位于# File vendor/rails/actionpack/lib/action_view/helpers/number_helper.rb, line 125
eg:number_with_delimiter(”162558855.3333233“)#162,558,855.3333233

10。
[color=blue] * number_to_currency
* number_to_human_size
* number_to_percentage
* number_to_phone
* number_with_delimiter
* number_with_precision[/color]
看看吧,在NumberHelper中!是关于数方方面的Helper.

11。在线制作LOGO,有倒影效果和Beat,[url]http://h-master.net/web2.0/index.php#home[/url]

12。将错误信息显示在自己想要的地方:

#写在helper里
def errors_for(object, attribute)
if errors = object.errors.on(attribute)
errors = [errors] unless errors.is_a?(Array)
return "<ul class='blad-form'>" + errors.map {|e| "<li>" + e + "</li>"}.join + "</ul>"
end
end

在页面上使用

<%= errors_for @model, :description %>


13。
ActionController::InvalidAuthenticityToken解决办法
在表单中
<%= tag(:input, :type => "hidden", :name => 
request_forgery_protection_token.to_s, :value =>
form_authenticity_token) %>


14。Rails中加密字符串
MD5加密:

require 'digest/md5'

puts Digest::MD5.hexdigest("Hello World!")


SHA1加密:

require 'digest/sha1'

puts Digest::SHA1.hexdigest("Hello World!")


————————————————————————
我把文章发到rails版论坛里,希望大家也发发自己的开发技巧,让大家都有所收获。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值