【问题标题】:Customize color for each show page - ruby on rails为每个显示页面自定义颜色 - ruby​​ on rails
【发布时间】:2017-02-15 04:07:40
【问题描述】:

我们有几个组织,每个组织都有一个 .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 还很陌生……我是否正确地考虑了这一点?有没有人有更好的实现方法的想法?

【问题讨论】:

    标签: css ruby-on-rails sass


    【解决方案1】:

    您可能需要一个 yield 块来完成此操作。

    layouts/application.html.erb:

    <style media="screen">
      .theme {
        background: #<%= content_for?(:theme) ? yield(:theme) : default_theme %> !important;
      }
    </style>
    

    在每个视图中:

    <% content_for :theme, @organization.theme %>
    

    查看了解文档yieldhttp://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield

    更多关于content_for: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2015-09-13
      相关资源
      最近更新 更多