Passing instance variable to stylesheet assets

…衆ロ難τιáo~ 提交于 2019-12-31 01:10:35

问题


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

Can I do something like this

preview_controller.rb

def show
 @design = Design.first
end

and my assets file is

preview.css.scss.erb

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

回答1:


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.



来源:https://stackoverflow.com/questions/8752142/passing-instance-variable-to-stylesheet-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!