一種方法是在你的佈局將控制器和動作名稱到body
標籤。然後,您可以依靠純CSS來提供不同的圖像。在您的佈局文件,你可以這樣做:
<%= content_tag :body, class: "#{controller_name}-#{action_name}" do %>
<!-- Your layout content, or everything that should be in the body tag goes here -->
<% end %>
這會給你下面的HTML:
<body class="items-index> <!-- or whatever the current controller/action are -->
然後,您可以添加相關的CSS。
.items-index {
background-image: image-url("items_index.jpg");
}
.pages-home {
background-image: image-url("pages_home.jpg");
}
你可以把它包裝在一個幫手裏,使它更清潔。
def class_for_body
"#{controller_name}-#{action_name}"
end
甚至:
def body_tag(&block)
content_tag :body, yield, class: "#{controller_name}-#{action_name}"
end
見的Rails提供controller_name
和action_name
方法。
「hom.jpg」是否可能不是資產的正確名稱? –