问题
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