资产管道中的 Rails 静态 html 模板文件和开发模式下的缓存 [英] Rails static html template files in the asset pipeline and caching in development mode

查看:27
本文介绍了资产管道中的 Rails 静态 html 模板文件和开发模式下的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AngularJS 和 Rails 构建网站.我用于模板的 HTML 文件存储在/app/assets/templates 下,每次更新路由或更改模板内部嵌套部分中的某些内容时,我都需要触摸"我正在更改的 html 文件的/app/assets/templates 目录.

因此,如果我有一个页面edit.html"加载了部分_form.html",那么每当我更新路线或更改 _form.html 中的某些内容时,我都需要确保触及 edit.html.

这很烦人,也很挑剔.有没有办法通知资产管道/链轮避免缓存应用/资产/模板目录?

解决方案

我发现的最佳解决方案是不对 HTML 模板文件使用资产管道.

而是创建一个名为 TemplatesController 的控制器并只创建一个操作.然后使用以下路由将所有模板 URL 映射到该 URL:

get/templates/:path.html =>'模板#page', :constraints =>{:路径=>/.+/}

然后将所有模板文件移动到app/views/templates

然后在控制器内部,设置以下内容:

caches_page :page定义页面@path = params[:path]渲染:模板=>'模板/' + @path, :layout =>零结尾

这样,您的所有模板文件都将从控制器提供,然后缓存到 public/templates 中.为避免缓存问题,您可以在模板路由中创建时间戳路径,以便您的缓存文件随版本一起交付:

get '/templates/:timestamp/:path.html' =>'模板#page', :constraints =>{:路径=>/.+/}

这样,您每次上传网站时都可以有一个新的时间戳,并且可以将模板文件夹存储在您喜欢的任何位置.您甚至可以将模板文件夹存储在 S3 上,并为此拥有一个资产 URL.然后,无论您的模板文件在哪里,您都可以使用自定义资产方法:

templateUrl : <%= custom_asset_template_url('some/file.html') %>

地点:

def custom_asset_template_url(path)http://custom-asset-server.website.com/templates/#{$some_global_timestamp}/#{path}"结尾

如果没有找到,那么只需将资产重定向到 Rails 服务器并生成它.或者所有的模板文件都可以在上传后预先生成.

I'm building a website using AngularJS and Rails. The HTML files that I'm using for templates are stored under /app/assets/templates and each time I update a route or change something inside of a nested partial inside of a template I need to "touch" the highest level file in the /app/assets/templates directory for the html file I'm changing.

So if I have a page "edit.html" which loads a partial "_form.html" then whenever I update a route or change something in _form.html I need to make sure that edit.html is touched.

This is annoying and very finicky. Is there any way to inform the asset pipeline/sprockets to avoid caching for the app/assets/templates directory?

解决方案

The best solution I've found to this is not to use the asset pipeline for HTML template files.

Instead make a controller called TemplatesController and create only one action. Then map all template URLs to that using a route such as:

get /templates/:path.html => 'templates#page', :constraints => { :path => /.+/  }

Then move all the template files into app/views/templates

Then inside the controller, setup the following:

caches_page :page

def page
  @path = params[:path]
  render :template => 'templates/' + @path, :layout => nil
end

This way all of your template files will be served from the controller and then will be cached into public/templates. To avoid cache problems, you can create a timestamp path into the template route so that your cached files are delivered with a version:

get '/templates/:timestamp/:path.html' => 'templates#page', :constraints => { :path => /.+/ }

This way you can have a new timestamp each time you upload the website and you can store the templates folder anywhere you like. You can even store the templates folder on S3 and have an assets URL for that. Then wherever your template files are addressed, you can use a custom asset method:

templateUrl : <%= custom_asset_template_url('some/file.html') %>

Where:

def custom_asset_template_url(path)
  "http://custom-asset-server.website.com/templates/#{$some_global_timestamp}/#{path}"
end

Then just make the asset redirect to the Rails server if it's not found and it will be generated. Or all template files can be pre-generated once uploaded.

这篇关于资产管道中的 Rails 静态 html 模板文件和开发模式下的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆