Don't worry about caching right now. Just see if it renders at all:
class Admin::StylesheetsController < ApplicationController
respond_to :css
def show
@stylesheet = Stylesheet.find(params[:id])
render text: @stylesheet.code, content_type: "text/css"
end
end
Write a test for GET /stylesheets/:id.css
. And in the markup:
<link rel="stylesheet" href="<%= stylesheet_path(@stylesheet, format: :css) %>" type="text/css" />
It seems like you're confusing the stylesheet for the forum. The stylesheet is a resource, and it is requested implicitly with the <link>
tag. You put the link to the stylesheet as part of the forum html page.
Your stylesheets/:id.css
path returns the css only, no markup. A test for this assuming the field in your stylesheet model is called :code
would be:
describe '#show' do
let(:stylesheet) { Factory(:stylesheet) }
it 'should have the correct css' do
get :show, id: stylesheet.id
expect(response.body).to eq(stylesheet.code)
end
end
Seems like you're confused about the way Rails works with requests. I'll walk you through the timeline:
Browser requests the forum page. This is handled by
ForumsController#show
or something like that.In a before filter or otherwise, you need to determine the id for the stylesheet somehow, for the
<link>
tag.Your layout application is wrapped around the page for the action, and the request ends.
Browser notices it needs to request stylesheets / javascripts, one of which is your custom stylesheet.
Your
StylesheetsController#show
is called, and the stylesheet is rendered back. This and all the other assets requests are completed.Now the user with the browser can see the custom css.
So you see, the @stylesheet
is not set when the page is rendered, and istance variables are set per controller per request. Not globally. Setting @stylesheet
in StylesheetsController#show
only sets it for the route GET /stylesheets/:id.css
.
You might want to consider using sublayouts - http://www.ajostrow.me/thoughts/sublayouts-with-rails-revised