|
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开发中也用得比较多,所以记一下。 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]elsetextendend#庄表伟的做法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 textend
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.
11。在线制作LOGO,有倒影效果和Beat,http://h-master.net/web2.0/index.php#home
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版论坛里,希望大家也发发自己的开发技巧,让大家都有所收获。 |
|