Rails - override <head> page element when using a partial or a specific controller view

瘦欲@ 提交于 2019-12-18 12:29:09

问题


Is there a way to append or override the <head> page element inside a rails view?

Suppose I have something I only want to include in a particular view's <head>, and I am using the application.html.erb in order to render the rest of my views. In this case I do not want to discard application.html.erb but instead just append to it's head element for one particular page and nothing else.


回答1:


In your application.html.erb:

<head>
  <% if content_for? :for_head %>
    <%= yield :for_head %>
  <% end %>

In your "specific" view:

<% content_for :for_head do %>
  Something-to-put-in-head
<% end %>

:for_head isn't predefined: naming it is up to you. It could be anything.



来源:https://stackoverflow.com/questions/10225645/rails-override-head-page-element-when-using-a-partial-or-a-specific-controll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!