mat*_*sko 9 ruby-on-rails asset-pipeline
我正在使用AngularJS和Rails构建一个网站.我用于模板的HTML文件存储在/ app/assets/templates下,每次更新路径或更改模板内部嵌套部分内的内容时,我都需要"触摸"最高级别的文件.我正在改变的html文件的/ app/assets/templates目录.
因此,如果我有一个页面"edit.html"加载部分"_form.html",那么每当我更新路线或更改_form.html中的内容时,我都需要确保触及edit.html.
这很烦人,非常挑剔.有没有办法通知资产管道/链轮,以避免app/assets/templates目录的缓存?
mat*_*sko 21
我发现的最佳解决方案是不要将资产管道用于HTML模板文件.
而是调用一个控制器TemplatesController并只创建一个动作.然后使用以下路由将所有模板URL映射到该路由:
get /templates/:path.html => 'templates#page', :constraints => { :path => /.+/ }
Run Code Online (Sandbox Code Playgroud)
然后将所有模板文件移动到 app/views/templates
然后在控制器内部,设置以下内容:
caches_page :page
def page
@path = params[:path]
render :template => 'templates/' + @path, :layout => nil
end
Run Code Online (Sandbox Code Playgroud)
这样,您的所有模板文件都将从控制器提供,然后将缓存到公共/模板中.为避免缓存问题,您可以创建模板路径的时间戳路径,以便使用以下版本传递缓存文件:
get '/templates/:timestamp/:path.html' => 'templates#page', :constraints => { :path => /.+/ }
Run Code Online (Sandbox Code Playgroud)
这样,每次上传网站时都可以使用新的时间戳,并且可以将模板文件夹存储在任何您喜欢的位置.您甚至可以将模板文件夹存储在S3上,并为其设置资产URL.然后,只要模板文件被寻址,您就可以使用自定义资产方法:
templateUrl : <%= custom_asset_template_url('some/file.html') %>
Run Code Online (Sandbox Code Playgroud)
哪里:
def custom_asset_template_url(path)
"http://custom-asset-server.website.com/templates/#{$some_global_timestamp}/#{path}"
end
Run Code Online (Sandbox Code Playgroud)
然后,只要将资产重定向到Rails服务器(如果找不到它),它就会生成.或者上传后可以预先生成所有模板文件.
有很多(更多!)更好的方法来解决这个问题.
<%= path_to_asset("template_name.html") %>
这将从资产管道返回一个完全工作的文件,它可以使用ERB等.它没有文档,但它是链轮/资产管道的一部分.
| 归档时间: |
|
| 查看次数: |
8942 次 |
| 最近记录: |