我们有几个组织,每个组织都有一个 .show.html.erb 页面。我们希望组织能够自定义其页面的颜色。
在我们的模式中,我们有:
create_table "organizations", force: :cascade do |t|
t.string "theme"
end
我尝试将以下内容添加到我们的“layouts/application.html.erb”页面以启用此自定义(在本地工作,但不在生产中):
<style media="screen">
.theme {
background: #<%= @organization.theme %> !important;
}
</style>
我是 Rails 的新手……我的想法是否正确?有没有人对更好的实现方法有任何想法?
最佳答案
您可能需要一个 yield
block 来完成此操作。
在 layouts/application.html.erb
中:
<style media="screen">
.theme {
background: #<%= content_for?(:theme) ? yield(:theme) : default_theme %> !important;
}
</style>
在每个 View 中:
<% content_for :theme, @organization.theme %>
查看理解yield
的文档:
http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield
关于 content_for
的更多信息:http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for
关于css - 为每个展示页面自定义颜色 - ruby on rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235054/