rails slim 動態 body attributes 範例 (利用 content_for)
利用 rails template 中的 content_for 在 slim 中實做動態 body_attributes

application_helper.rb
application.html.slim
view 裡面使用方法
def html_body_attributes
content_for?(:body_attributes) ? ::Nokogiri::XML("<x>#{content_for(:body_attributes)}</x>").xpath('//x').first.children.each_with_object({}) { |node, obj| obj[node.name] = node.text } : {}
end
application.html.slim
body class=(yield :body_classes) *html_body_attributesclass=(yield :body_classes) 的部份可以忽略,因為我個人會把 <body> class 獨立出來用別的方式給值。
view 裡面使用方法
- content_for(:body_attributes) do data-turbolinks="false" data-lazy-loading="true"每個 attribute 之間需要換行,slim 會以 xml 格式輸出,helper 中再 parse XML 以 ruby hash 輸出即可。
延伸閱讀
加入 BNI 商會心得
於 2023/10 加入了 BNI 台南商會(大信白金),本篇文章將分享加入後的心得,並持續更新心得內容,確認在不同階段的自己是否有偏離初衷。
rails app(web) 嵌入到 iframe 中
ruby on rails 的 web 預設是無法嵌入到 HTML iframe 中的,本篇文章將教學如何使其成功在不同網域中將 rails web 成功嵌入到 iframe 中。
設定電子郵件 DMARC
提供設定 DMARC 的範例,並且提供官方語法說明和檢測工具連結。

