Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom CSS via controller returning via #show/n?format=css but not #show/n.css
    primarykey
    data
    text
    <p>I am implementing a custom theming issue for a Rails 3.2 app, with a very small number of CSS overrides for each theme.</p> <p>While I have so far implemented the theme variations by inlining some CSS (including occasional Erb parameters) within the layout file, I'd really like to tidy this up by delivering the customisation CSS via a GET request, e.g.: (assume <code>current_theme</code> is defined)</p> <pre><code>&lt;%= stylesheet_link_tag theme_path(current_theme, format: :css) %&gt; </code></pre> <p>In my themes_controller.rb:</p> <pre><code>class ThemesController &lt; ApplicationController respond_to :css ... def show @theme = Theme.find(params[:id]) respond_with @theme end end </code></pre> <p>I have the requisite <code>show.css.erb</code> file in <code>views/themes</code>.</p> <p>My main issue is that <code>/themes/1?format=css</code> loads and renders the CSS file correctly. However, <code>/themes/1.css</code> – the form of URL generated by the <code>theme_path</code> helper – is producing a 404.</p> <p>I'm probably overlooking something <em>very</em> simple here – hopefully a fellow SO user can point out the bloomin' obvious to prevent my head and brick walls becoming better acquainted...</p> <p><strong>UPDATE:</strong> Doing a bot of Rails route recognition debugging:</p> <pre><code>r = Rails.application.routes #=&gt; #&lt;ActionDispatch::Routing::RouteSet:0x007fc385016170&gt; r.recognize_path '/themes/1.css' #=&gt; {:action=&gt;"show", :controller=&gt;"themes", :id=&gt;"1", :format=&gt;"css"} r.recognize_path '/themes/1?format=css' #=&gt; {:action=&gt;"show", :controller=&gt;"themes", :id=&gt;"1"} </code></pre> <p>So the fact that <code>?format=css</code> seems to be working is actually because, at least when CURLing from the command line, <code>/themes/1</code> is returning the requisite CSS. As far as I'm concerned, that's a red herring...</p> <p>Incidentally, adding this line to <code>routes.rb</code> works:</p> <pre><code> get '/themestyle/:id' =&gt; 'themes#show', as: 'themestyle', format: 'css' </code></pre> <p>(using a slightly different route so as not to conflict with the <code>resources :themes</code> in routes.rb). <code>themestyle_path(current_theme)</code> generates a workable route of – <code>/themestyle/1</code> – I can add the 'text/css' type header at render stage no problem. I'll have to hand-code a <code>&lt;link rel="stylesheet"&gt;</code> element though, as stylesheet_link_tag adds the css suffix.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload