我正在建立一个论坛,我希望管理员能够在管理控制面板中编辑 css(就像大多数 PHP 论坛一样)。
我已将 css 存储在一个名为样式表的表中。
我需要能够在布局/应用程序的样式表中调用此特定行,以便 css 显示在所有页面上。
我想知道是否有一种方法可以使用 stylesheet_link_tag 来调用数据库表中 css 的位置?
如果不是……我该怎么做?
我试过用这个:Best way to handle dynamic css in a rails app但它不起作用。
编辑:我要做的只是调用数据库而不是调用资产/样式表/布局(<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>) /样式表/id1.code。
我只需要获取 ID 1 并从中提取代码并将其显示为每个页面作为主 CSS。
这是我现在得到的错误:
没有路由匹配 {:controller=>"stylesheets", :action=>"show", :id=>nil, :format=>:css} 缺少必需的键:[:id]
样式表控制器
class Admin::StylesheetsController < ApplicationController
    caches_page :show # magic happens here
    def index
        @stylesheets = Stylesheet.all
  end
    def show
        @stylesheet = Stylesheet.find(params[:id])
        respond_to do |format|
          format.html # regular ERB template
          format.css { render :text => @stylesheet.code, :content_type => "text/css" }
        end
    end
  def edit
    @stylesheet = Stylesheet.find(params[:id])
  end
    # When you edit/update the category, update the information
    def update
        @stylesheet = Stylesheet.find(params[:id])
        if @stylesheet.update_attributes(params[:stylesheet].permit!)
            redirect_to edit_stylesheet_path
        else
            render :edit
        end
    end
end
布局/Application.html.erb
<!DOCTYPE html>
<html>
<head>
  <title>Ruby Ibis | <%= yield(:title) %></title>
    <link rel="stylesheet" href="<%= stylesheet_path(@stylesheet, format: :css) %>" type="text/css" />
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
   <%= render 'layouts/shim' %>
  </head>
   <body>
    <%= render 'layouts/header' %>
        <div class="container">
      <% flash.each do |key, value| %>
        <div class="alert alert-<%= key %>"><%= value %></div>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html> 
测试结果
$ rspec spec/controllers/stylesheets_controller_spec.rb ←[31mF←[0m
失败:
1) StylesheetsController#show 应该有正确的 css ←[31mFailure/Error:←[0m ←[31mlet(:stylesheet) { Factory(:stylesheet) }←[0m ←[31mNoMethodError←[0m: ←[31mundefined 方法
Factory' for #<RSpec::Core::ExampleGroup::Nested_1 ::Nested_1:0x47793f0>←[0m ←[36m # ./spec/controllers/stylesheets_controller_spec.rb:42:in块(3级别) in '←[0m ←[36m # ./spec/controllers/stylesheets_controller_spec.rb:44:in `block (3 级别) in '←[0m0.02秒完成←[31m1例,1次失败←[0m
失败的例子:
←[31mrspec ./spec/controllers/stylesheets_controller_spec.rb:43←[0m ←[36m# Style sheetController#show应该有正确的css←[0m
随机种子 18359