网页好读版:http://ruby-china.org/topics/7315
## Rails Guide Style
在 [Rails Guide 上面](http://goo.gl/Vysga)有这样的写法:
<%= javascript_include_tag params[:controller] %>
or
<%= stylesheet_link_tag params[:controller] %>
但是这样写的话,在 production 时一定会报错,
所以要在 `config.assets.precompile` 加上不少东西,类似:
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
}
**然而这在 Rails Guide 上面并没有提醒我们要做这件事情**
(所以等等想贡献一下提示到 docrails 这个 repo 上)
## Garber Irish Style
然而有另外一种方案:
http://goo.gl/7bu5X
使用起来的话,以 `posts.js.coffee` 为例子:
# posts.js.coffee
SITENAME.posts =
index: () ->
# blablabla
show: () ->
# blablabla
new: () ->
# blablabla
edit: () ->
# blablabla
update: () ->
# blablabla
我觉得很有条理,而且比上述的 Rails Guide style 简单很多,
如果要共用重复的代码也可以这么做:
SITENAME.posts =
index: () ->
this.do_something()
show: () ->
this.do_something()
new: () ->
this.do_something()
edit: () ->
this.do_something()
update: () ->
this.do_something()
do_something () ->
#blablabla
因为管理方便,所以最近的专案都开始使用这个方法了。