css - 为每个展示页面自定义颜色 - ruby​​ on rails

标签 css ruby-on-rails sass

我们有几个组织,每个组织都有一个 .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/

相关文章:

ruby-on-rails - Elasticsearch 和轮胎

css - Bootstrap 响应式 CSS

CSS:加载微调器方形圆形外观

javascript - 在给定时间后反转 jQuery 效果

ruby-on-rails - 如何定义返回带字符串键的哈希的 Factory Girl 工厂?

css - WebStorm 在 TFS 上的 scss 文件观察器(使用 node-sass)上清除 css 文件的只读标志

twitter-bootstrap - 相对于 Scss $font-size-base 更改 Bootstrap4 字体大小

html - 如何在 Bootstrap 中的图像顶部添加列

html - 嵌套 div 内的 CSS 填充

mysql - 销毁操作从错误的表中删除