浏览 698 次
|
精华帖 (0) :: 良好帖 (9) :: 新手帖 (3) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2008-07-25 关键字: 技巧 ruby rails
Ruby On Rails开发时的技术还是很多很杂的,我现在把它们整理,列出来,方便自己,也方便大家。
注:我在其中所列的,也有不是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的作用一样,另说明一下,这个网站上可以在线为我们生成自行设置的图片,很方便,http://www.buttonator.com/,我又看了一下这个网站,感觉它也很优秀,http://www.mycoolbutton.com 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.javaeye.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方法定义如下 :
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中有number_with_delimiter(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。 * number_to_currency * number_to_human_size * number_to_percentage * number_to_phone * number_with_delimiter * number_with_precision 看看吧,在NumberHelper中!是关于数方方面的Helper. ———————————————————————— 我把文章发到rails版论坛里,希望大家也发发自己的开发技巧,让大家都有所收获。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |




: