title: Using stylesheet_link_tag to call css in a database [Rails 4]
body: <p>I am building a forum and I wish for the admins to be able to edit the css in the admin control panel (like most PHP forums). </p>
  
  <p>I've stored the css in a table called stylesheet. </p>
  
  <p>I need to be able to call this specific row in the stylesheets table in layout/applications
  so the css shows on ALL pages.</p>
  
  <p>I was wondering if there is a way of using the stylesheet_link_tag to call the location of the css inside the database table?</p>
  
  <p>If not... How exactly do I do it?</p>
  
  <p>I tried using this: <a href="https://stackoverflow.com/questions/4729282/best-way-to-handle-dynamic-css-in-a-rails-app">Best way to handle dynamic css in a rails app</a> but it did not work. </p>
  
  <p>EDIT: All I am trying to do is instead of calling the assets/stylesheets/layout (&lt;%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>) <strong>I call the database/stylesheet/id1.code.</strong></p>
  
  <p>I just need to get ID 1 and extract the code from it and display it for every single page as the main CSS.</p>
  
  <p>This is the error I get now: </p>
  
  <blockquote>
    <p>No route matches {:controller=>"stylesheets", :action=>"show", :id=>nil, :format=>:css} missing required keys: [:id]</p>
  </blockquote>
  
  <p><strong>Stylesheet Controller</strong></p>
  
  <pre><code>class Admin::StylesheetsController &lt; 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 =&gt; @stylesheet.code, :content_type =&gt; "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
  </code></pre>
  
  <p><strong>Layout/Application.html.erb</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Ruby Ibis | &lt;%= yield(:title) %&gt;&lt;/title&gt;
  
      &lt;link rel="stylesheet" href="&lt;%= stylesheet_path(@stylesheet, format: :css) %&gt;" type="text/css" /&gt;
  
    &lt;%= javascript_include_tag "application", "data-turbolinks-track" =&gt; true %&gt;
    &lt;%= csrf_meta_tags %&gt;
     &lt;%= render 'layouts/shim' %&gt;
  
    &lt;/head&gt;
     &lt;body&gt;
      &lt;%= render 'layouts/header' %&gt;
          &lt;div class="container"&gt;
        &lt;% flash.each do |key, value| %&gt;
          &lt;div class="alert alert-&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt;
        &lt;% end %&gt;
        &lt;%= yield %&gt;
        &lt;%= render 'layouts/footer' %&gt;
        &lt;%= debug(params) if Rails.env.development? %&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>
  
  <p><strong>Results of test</strong></p>
  
  <blockquote>
    <p>$ rspec spec/controllers/stylesheets_controller_spec.rb ←[31mF←[0m</p>
    
    <p>Failures:</p>
    
    <p>1) StylesheetsController#show should have the correct css
         ←[31mFailure/Error:←[0m ←[31mlet(:stylesheet) { Factory(:stylesheet) }←[0m
         ←[31mNoMethodError←[0m:
           ←[31mundefined method <code>Factory' for #&lt;RSpec::Core::ExampleGroup::Nested_1 ::Nested_1:0x47793f0&gt;←[0m ←[36m     # ./spec/controllers/stylesheets_controller_spec.rb:42:in</code>block (3 le vels) in '←[0m ←[36m     #
    ./spec/controllers/stylesheets_controller_spec.rb:44:in `block (3 le
    vels) in '←[0m</p>
    
    <p>Finished in 0.02 seconds ←[31m1 example, 1 failure←[0m</p>
    
    <p>Failed examples:</p>
    
    <p>←[31mrspec ./spec/controllers/stylesheets_controller_spec.rb:43←[0m
    ←[36m# Style sheetsController#show should have the correct css←[0m</p>
    
    <p>Randomized with seed 18359</p>
  </blockquote>