2008-07-25

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

关键字: 技巧 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

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

————————————————————————
我把文章发到rails版论坛里,希望大家也发发自己的开发技巧,让大家都有所收获。
  • noisy_image.rar (697 Bytes)
  • 描述: 将这个rb文件放在/app/models里,然后在某一个controller中定义一个方法如下:[code="ruby"] 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 => 'i
  • 下载次数: 10
  • 784b9247-d1a2-37ae-b0a1-f60f9855e10b-thumb
  • 描述: 产生的图片验证码
  • 大小: 2 KB
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

qichunren
  • 浏览: 9304 次
  • 性别: Icon_minigender_1
  • 来自: 与JE一起成长
  • 详细资料
搜索本博客
我的相册
85da3bd0-a62a-3ed8-b828-67195e4ce6d2-thumb
qq.png
共 4 张
存档
最新评论