How to respond with a stylesheet from your Rails controller?

问题: I have a Rails 5.2.0 api app with a limited set of middleware. From the app I want to serve a css file to browsers requesting it. The content of the css file depends on t...

问题:

I have a Rails 5.2.0 api app with a limited set of middleware. From the app I want to serve a css file to browsers requesting it.

The content of the css file depends on the request params, so I'd like to be able serve the file from a controller. I already do this successfully with a script. Here is my implementation:

Rails.application.routes.draw do
  get "assets/script"     => "assets#script"
  get "assets/stylesheet" => "assets#stylesheet", :format => :css
end

class AssetsController < ApplicationController

  ## This action works
  def script
    js = ActionController::Base.render(
      "assets/script", assigns: {foo: params[:foo]}
    )
    render :js => js
  end

  ## This action doesn't work
  def stylesheet
    css = ActionController::Base.render(
      "assets/stylesheet", assigns: {foo: params[:foo]}
    )
    render :plain => css
  end
end

When a browser requests the script it becomes available in the browser:

<script>$.getScript("https://example.com/assets/script");</script>

However that is not the case with my stylesheet:

$('<link/>', {
  rel: 'stylesheet',
  href: "https://example.com/assets/stylesheet.css"
}).appendTo('head');

The stylesheet is returned and added in the browser, but it isn't put to use. I can serve the exact same content from the /public folder of a regular Rails app, and it will be put to use.

I must be missing something, maybe some response headers. Any idea what I'm missing?


回答1:

Browsers expect css to be served with corresponding content type:

render body: css, content_type: 'text/css'

Rails has default mime type for this: render css: css

Also you can render in one step:

render template: "assets/stylesheet", formats: [:css], assigns: {foo: params[:foo]}
  • 发表于 2019-03-12 01:07
  • 阅读 ( 158 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除