将实例变量传递给样式表资产 [英] Passing instance variable to stylesheet assets

查看:50
本文介绍了将实例变量传递给样式表资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将实例变量从我的控制器传递到我的资产样式表?

How do I pass an instance variable from my controller to my assets stylesheet?

我可以这样做吗

preview_controller.rb

preview_controller.rb

def show
 @design = Design.first
end

我的资产文件是

preview.css.scss.erb

preview.css.scss.erb

body{
  background-image: url('<%= @design.image_url.to_s %>');
}

推荐答案

您不能这样做(资产是预编译的,无法访问来自请求的变量).你可以在你的 HTML 页面中使用它:

You can't do this (the assets are pre-compiled and do no have access to variables from the request). You could have this in your HTML page itself:

<head>
  <%= stylesheet_link_tag "application" %>
  <style>
  body{
    background-image: url('<%= @design.image_url.to_s %>');
  }
  </style>
</head>
<body>
..

它会达到相同的最终结果.

It would achieve the same end result.

这篇关于将实例变量传递给样式表资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆