如何使用 Rails caches_action 和 layout:false 并动态更改元标记?
How to use Rails caches_action with layout:false and dynamically change meta tags?
我被困在 Rails 网络应用程序中我认为非常 simple/common 用例。我想使用“caches_action、layout:false”并从布局中显示将由操作(从视图或控制器)设置的动态标签。
我找不到任何标准的 rails 方法来执行此操作,因为 content_for 不适用于 caches_action ,不缓存实例变量(?),我尝试过的元标记助手 gem(metamagic and meta-tags)不支持这个用例。
有什么办法吗?
例子
我在 SandboxController#show 方法上使用 caches_action、layout:false
#app/controllers/sandbox_controller.rb
class SandboxController < ApplicationController
caches_action :show, layout: false, expires_in: 1.minute
def show
@meta_title = "Best page ever"
do_some_expensive_operation
end
end
景色
#app/views/sandbox/show.html.erb
We are in show action.
布局
#app/views/layouts/application.html.erb
<title><%= @meta_title %></title>
Debug: <%= @meta_title %> <br/>
<%= yield %>
谢谢!
我找到了一种让它工作的方法,它不像我希望的那样漂亮,但它有助于使用 caches_action 并从视图中设置 HTML 元标记。
此外,作为记录,这似乎被遗忘并埋藏在管道深处,因为我没有找到 any recent mentions of this problem, only that caches_action and content_for together are not expected to work.
解决方案:我只是添加了一个 before_action 来设置元标记,使用尽可能少的计算。
#app/controllers/sandbox_controller.rb
class SandboxController < ApplicationController
caches_action :show, layout: false, expires_in: 1.minute
before_action :seo_show, only: :show
def seo_show
@meta_title = "Best page ever"
end
def show
do_some_expensive_operation
end
end
值得注意的是,它也可以与 metamagic gem 结合使用。
布局:
#app/views/layouts/application.html.erb
<%= default_meta_tags && metamagic %>
<%= yield %>
和帮手:
#app/helpers/application_helper.rb
module ApplicationHelper
def default_meta_tags
meta title: @meta_title || "Default meta-title of my website"
end
end
希望这对外面的人有帮助!
我被困在 Rails 网络应用程序中我认为非常 simple/common 用例。我想使用“caches_action、layout:false”并从布局中显示将由操作(从视图或控制器)设置的动态标签。
我找不到任何标准的 rails 方法来执行此操作,因为 content_for 不适用于 caches_action ,不缓存实例变量(?),我尝试过的元标记助手 gem(metamagic and meta-tags)不支持这个用例。
有什么办法吗?
例子
我在 SandboxController#show 方法上使用 caches_action、layout:false
#app/controllers/sandbox_controller.rb
class SandboxController < ApplicationController
caches_action :show, layout: false, expires_in: 1.minute
def show
@meta_title = "Best page ever"
do_some_expensive_operation
end
end
景色
#app/views/sandbox/show.html.erb
We are in show action.
布局
#app/views/layouts/application.html.erb
<title><%= @meta_title %></title>
Debug: <%= @meta_title %> <br/>
<%= yield %>
谢谢!
我找到了一种让它工作的方法,它不像我希望的那样漂亮,但它有助于使用 caches_action 并从视图中设置 HTML 元标记。
此外,作为记录,这似乎被遗忘并埋藏在管道深处,因为我没有找到 any recent mentions of this problem, only that caches_action and content_for together are not expected to work.
解决方案:我只是添加了一个 before_action 来设置元标记,使用尽可能少的计算。
#app/controllers/sandbox_controller.rb
class SandboxController < ApplicationController
caches_action :show, layout: false, expires_in: 1.minute
before_action :seo_show, only: :show
def seo_show
@meta_title = "Best page ever"
end
def show
do_some_expensive_operation
end
end
值得注意的是,它也可以与 metamagic gem 结合使用。
布局:
#app/views/layouts/application.html.erb
<%= default_meta_tags && metamagic %>
<%= yield %>
和帮手:
#app/helpers/application_helper.rb
module ApplicationHelper
def default_meta_tags
meta title: @meta_title || "Default meta-title of my website"
end
end
希望这对外面的人有帮助!