ROR一些技巧
1.更改默认首页
Rails要求URL需要以 http://localhost:3000/controller_name/action_name 的形式,由此我们似乎无法存放我们的主页,其实我们要做的只是修改 routes.rb(config/routes.rb)
1.route:
routes.rb文件中,写入类似语句 map.connect ':controller/:action/:id' ,进行routing,习惯上称其为 route
2.优先级:先创建的 route 有高的优先级
3.修改http://localhost:3000/ 指向的controller
1)先删除public下面的index.rhtml
2)添加一个route:
map.connect '', :controller => "controller_name"
2.简单的块和迭代器
Easier collections with to_proc - Sick of writing things like post.collect { |p| p.title } or post.select { |p| p.activated? }.collect{ |p| p.title} ? A little Ruby hackery that allows you to convert symbols into proc references makes it easy. You can write post.collect(&:title) or post.select(&:activated?).collect(&:title) instead! Learn a lot more about this.
糟糕的写法就象post.collect { |p| p.title } 或 post.select { |p| p.activated? }.collect{ |p| p.title}
Ruby允许使用符号来简化操作。你能用post.collect(&:title) 或post.select(&:activated?).collect(&:title)来代替。
当想把字符串从小写转变成大写的时候一般会这么做:
result = names.map {|name| name.upcase}使用符号可以改写成:
result = names.map(&:upcase)
3.使用to_sentence方法转变数组
Convert arrays to sentences in views - If you were collecting a bunch of names to be shown in a view, you might end up with an array like ['Peter', 'Fred', 'Chris'], and joining these with commas and inserting 'and' before the final one is a common pain. Not so, if you use the array method to_sentence as provided in Rails. names.to_sentence would return Peter, Fred, and Chris.
当你正在处理一个串名字的时候,你可能会用数组来解决问题,你或许会用逗号间隔它们,并在最后一个元素前加上’和’,如果你使用to_sentence方法就能大大简化这一操作。
Temp = %w{张三 李四 和 王五}
一般用用这样的方式来输出张三,李四和王五,可以使用to_sentence方法来简化这一操作
Temp = %w{张三 李四 王五}
Puts temp. to_sentence
使用to_param可以输出张三/李四/王五
4.判断数据是否存在
Check for existence - When doing a Model.find(id), an exception can be returned if the item with an id of 'id' doesn't exist. If you want to avoid this, use Model.exists?(id) first to get a true or false for whether that item exists or not.
用模型.find(id)来查询数据库中的数据,怎样避免查询出它不存在呢?可以使用Model.exists?(id) 如:Person.exists?(5)
5.使用Number helper来格式化数据
Number helpers for common number tasks - All of these number helpers aren't commonly used but provide great shortcuts: number_to_currency(1234567.948) # => $1,234,567.95 or human_size(1234567890) # => 1.1GB or number_with_delimiter(999999999) # => 999,999,999.
There are others.
方法列表如下:
human_size
number_to_currency
number_to_human_size
number_to_percentage
number_to_phone
number_with_delimiter
number_with_precision
效果分别如下:
human_size(1234567890) => 1.1 GB
number_to_currency(1234567890) => $1,234,567,890.00
number_to_human_size(1234567890) => 1.1 GB
number_to_percentage(1234567890) => 1234567890.000%
number_to_phone(1234567890) => 123-456-7890
number_with_delimiter(1234567890) => 1,234,567,890
number_with_precision(1234567890) => 1234567890.000
使用时需用到ruby安装目录下的该helpers
%ruby%\lib\ruby\gems\1.8\gems\actionpack-1.12.5
\lib\action_view\helpers\number_helper.rb
6.取得request的一些有用信息
Get lots of info about requests - Checking request.post? and request.xhr? are popular ways to look for POST and AJAX requests, but some of the other request methods are lesser used. For example: request.subdomains can return an array of subdomains that you could use as part of your authentication scheme, request.request_uri returns the full local request URL, request.host returns the full hostname, request.method returns the HTTP method as a lowercase symbol, and request.ssl? returns true if it's an HTTPS / SSL request.
Request的一些方法展示:
accepts
content_type
delete?
domain
formatted_post?
get?
head?
host
host_with_port
method
parameters
path
path_parameters
port
port_string
post?
post_format
protocol
put?
raw_post
relative_url_root
remote_ip
request_uri
server_software
ssl?
standard_port
subdomains
symbolized_path_parameters
xhr?
xml_http_request?
xml_post?
yaml_post?
参考Class
ActionController::AbstractRequest
7.计算平均,最大,最小的方法。
average
calculate
count
maximum
minimum
sum
参考ActiveRecord::Calculations::ClassMethods
8将数据导出成xml或YAML
XML or YAML output of your data - It's not necessarily to create a Builder .rxml template for all XML output. ActiveRecord has a to_xml method that will output the object or result set in XML format. It works with simple objects, to complete tables (like User.find(:all).to_xml). Using includes works too, as with Post.find(:all, :include => [:comments]).to_xml. YAML is also supported, by using to_yaml instead.
不必去建立一个.rxml模板去导出XML,ActiveRecord有一个to_xml方法可以将输出对象或请求格式化成XML,它对简单的对象起作用,完成表格类似于User.find(:all).to_xml 同样的Post.find(:all, :include => [:comments]).to_xml也同样支持


