2017-02-14 70 views
0

我们有几个组织,每个组织都有一个.show.html.erb页面。我们希望使组织能够自定义页面的颜色。为每个显示页面自定义颜色 - 轨道上的红宝石

在我们的模式,我们有:

create_table "organizations", force: :cascade do |t| 
    t.string "theme" 
end 

我尝试添加以下我们的“布局/ application.html.erb页面启用此定制(本地工作,但不是在生产中):

<style media="screen"> 
    .theme { 
    background: #<%= @organization.theme %> !important; 
    } 
</style> 

我对Rails很新奇......我是否正确地考虑过这个问题?有没有人有更好的方式来实现这个想法?

回答

2

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

layouts/application.html.erb

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

在各视图:

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

见的理解yield文档: http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield

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