分拆routes.rb

2012.03.12
    项目需要给移动平台提供API,在routes.rb中已经配置了很多resources,如果再加入API的resources会使得routes.rb变得非常庞大,不利于管理,解决办法当然是希望能将API的resources写到另一个文件中去,如:mobile_routes.rb。
    rails提供了相当方便的扩展,你可以这样实现:
在application.rb中加入:
config.paths["config/routes"] += Dir[Rails.root.join("config/routes/*.rb")]
在config中创建routes文件夹,将mobile_routes.rb放到routes文件夹下即可,rails会自动将routes文件夹下的文件加载。
mobile_routes.rb文件的写法与routes.rb文件一样!
 

class Base < ActiveRecord::Base
  establish_connection(MYSQL_CFG)
end


 

ROR开发小技巧

2011.12.25

ruby-1.9.2-p180 :055 > File.join("lib/","ruby","gem.rb")
 => "lib/ruby/gem.rb"

ruby-1.9.2-p180 :058 > [1,2,3] - [1,2]
 => [3] 

ruby-1.9.2-p180 :059 > [1,2,3] & [1,2]
 => [1, 2] 

ruby-1.9.2-p180 :060 > [1,2,3] + [1,2,3,4]
 => [1, 2, 3, 1, 2, 3, 4]

ruby-1.9.2-p180 :066 > %w(a d c d 1 2 3)
 => ["a", "d", "c", "d", "1", "2", "3"]

ruby-1.9.2-p180 :074 > Hash["a",1,"b",2]
 => {"a"=>1, "b"=>2} 

ruby-1.9.2-p180 :075 > Hash[*["a",1,"b",2]]
 => {"a"=>1, "b"=>2} 

ruby-1.9.2-p180 :075 > Array("a".."z").select.with_index {|x,i| i%3 == 2}
 => ["c", "f", "i", "l", "o", "r", "u", "x"]

# coding:utf-8
# unicode转义
"" << 0x6f22 #=> 漢

class Object
  # returns instance methods of current object's class and its singleton methods
  def lm # abbr of local methods
    self.methods.sort - self.class.superclass.instance_methods
  end
end

参考:http://ruby-china.org/topics/541
 
ERB::Util 
activesupport/lib/active_support/core_ext/string/output_safety.rb
html_escape(s)

A utility method for escaping HTML tag characters. This method is also aliased as h.

In your ERB templates, use this method to escape any unsafe content. For example:

<%=h @person.name %>

Example:

puts html_escape("is a > 0 & a < 10?")
# => is a &gt; 0 &amp; a &lt; 10?
Also aliased as: h
# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 18
def html_escape(s)
  s = s.to_s
  if s.html_safe?
    s
  else
    s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;").html_safe
  end
end

ActionView::Helpers::JavaScriptHelper actionpack/lib/action_view/helpers/javascript_helper.rb
escape_javascript(javascript)

Escape carrier returns and single and double quotes for JavaScript segments. Also available through the alias j(). This is particularly helpful in JavaScript responses, like:

$('some_element').replaceWith('<%=j render 'some/element_template' %>');
Also aliased as: j
# File actionpack/lib/action_view/helpers/javascript_helper.rb, line 19
def escape_javascript(javascript)
  if javascript
    result = javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) {|match| JS_ESCAPE_MAP[match] }
    javascript.html_safe? ? result.html_safe : result
  else
    ''
  end
end

 

in_groups_of

2011.12.02
in_groups_of 这个方法可以将数组分组,的第一个参数指示几个元素一组,而第二个参数指示了当最后一组缺元素时用什么填补位置
%w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g}
["1", "2", "3"]
["4", "5", "6"]
["7", nil, nil]

%w(1 2 3).in_groups_of(2, ' ') {|g| p g}
["1", "2"]
["3", " "]

%w(1 2 3).in_groups_of(2, false) {|g| p g}
["1", "2"]
["3"]

<table>
  <% @tasks.in_groups_of(4) do |row_tasks| %>
    <tr>
    <% for task in row_tasks %>
      <td><%= task.name %></td>
    <% end %>
    </tr>
  <% end %>
</table>

 

Home Blog Delicious Github Flickr About Contact

© Miclle.Zheng . Powered by Forest Chalet