简体   繁体   English

Ruby on Rails,背景图片第一次不起作用

[英]Ruby on rails, background image not working the first time

I have the follow code: 我有以下代码:

<div class="photoProfile" style="background-image: url(<%= @user.image.url %>)"></div>

the variable @user.image.url always has value, but the first time, is not attached to the background-image, I need refresh the page and now the image is loaded perfectly. 变量@user.image.url始终具有值,但是第一次未附加到背景图像,我需要刷新页面,现在图像已完美加载。

Some one has and idea of this issue? 有人对这个问题有想法吗?

I have the same issue, and change "asset_path" to "asset_url". 我有同样的问题,然后将“ asset_path”更改为“ asset_url”。

Before) <div style="background-image: url(<%= **asset_path**(get_img(object)) %>)"> 之前) <div style="background-image: url(<%= **asset_path**(get_img(object)) %>)">

After) <div style="background-image: url(<%= **asset_url**(get_img(object)) %>)"> 之后) <div style="background-image: url(<%= **asset_url**(get_img(object)) %>)">

but, I cant't understand why it works. 但是,我不明白为什么会这样。

try this. 尝试这个。

<%= content_tag(:div, content_tag(:p, "Hello world!"), class: "photoProfile", style: "background-image: url('#{@user.image.url'})") %>

warning just a guess: I'm not sure but I guess that html and css is loaded before anything else. 警告只是一个猜测:我不确定,但是我猜html和css首先加载。 Basically the db hasn't fetched the data yet and html doesn't know what to do with this "string". 基本上,数据库尚未提取数据,而html尚不知道如何处理此“字符串”。 If we can manage to load everything in time as erb, it should be possible to show the image? 如果我们可以设法将所有内容及时加载为erb,那么应该可以显示图像吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM