【发布时间】:2014-11-29 15:09:55
【问题描述】:
我正在实施“功能开关”,以便管理员可以使用管理员 Web 界面打开和关闭新功能。
我可以在诸如
之类的视图中轻松做到这一点- if FeatureSwitch.where(name: 'display_demo_feature_switch_image', status: 'on').count > 0
# in reality would refactor that query to the controller/model. put here for clarity.
%br
%br
%div{class: "onoffswitch"}
%input{type: "checkbox", name: "onoffswitch", class: "onoffswitch-checkbox", id: "myonoffswitch"}
%label{class: "onoffswitch-label", for: "myonoffswitch"}
%span{class: "onoffswitch-inner"}
%span{class: "onoffswitch-switch"}
但是,当我使用资产管道并使资产“有条件地”包含在内时,如何在视图中执行此操作?
所以,如果我有一个 css 资产清单 (app/assets/stylesheets/application.css):
/*
*= require_self
*= require main
*= require default
*= require on_off_switch
*= require jquery-ui-1.8.22.custom.css
*/
我怎样才能使require on_off_switch“有条件”,这取决于来自的真/假返回
FeatureSwitch
.where(name: 'display_demo_feature_switch_image', status: 'on').count > 0
我尝试将 application.css 重命名为 application.css.erb 并使用
<% if 'abc' == 'def' %> # While developing/testing
*= require on_off_switch
<% end %>
但是尽管'abc' == 'def' 是假的,但资产总是包含在内...
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 conditional asset-pipeline assets