【发布时间】:2016-04-14 16:10:06
【问题描述】:
如果用户从他们的个人资料页面添加了指向图片的链接,我有以下代码来更改页面的背景图片。但是,我希望这适用于所有页面,而不仅仅是代码当前所在的页面。
我知道我可以将代码分别放在每个页面中,但这显然并不理想。我尝试将它放在 application.js 和 application.html.erb 文件中,但没有成功。
我相信这与 gon 使这些变量仅可用于它们所在的页面有关吗?
$(document).ready(function (){
if(gon.backgroundPicture) {
var background = gon.backgroundPicture;
document.body.style.backgroundImage = "url(" + background + ")";
};
});
<% if current_user.try(:student?) || current_user.try(:supervisor?) %>
<% if current_user.background_picture != :null %>
<% @gon.backgroundPicture = current_user.background_picture %>
<% end %>
<% end %>
【问题讨论】:
标签: javascript jquery ruby-on-rails ruby-on-rails-4