【问题标题】:set a background image with paperclip on rails 4在rails 4上使用回形针设置背景图像
【发布时间】:2015-04-07 20:11:12
【问题描述】:

好吧,我在用回形针获取背景图像时遇到了一些问题,实际上我得到了这个

在 app/views/layouts/application.html.erb

<head>
  ...
</head>
<body style="<%= show_user_bg %>">
  ...
</body>

在 app/helpers/application_helper.rb 中

module ApplicationHelper
  def show_user_bg
    "background:transparent url(#{@user.background_image}) no-repeat fixed left top;"
  end
end

还有这个模块助手

module ApplicationHelper
  def show_user_bg
    # Show user background
    if user_signed_in?
      "background:transparent url(#{@user.background_image}) no-repeat fixed left top;"
    # Otherwise, show a default background image
    else
      "background:transparent url('/images/default_bg.png') no-repeat fixed left top;"
    end
  end
end

但是,当我尝试访问图像时,我不知道从哪里获得它。

简历,我想要这个

<%= image_tag(@user.background.url, :width => "100%") %>

到这里

<div style="background:transparent url(#{@user.background.url}) no-repeat fixed left top;">

任何帮助将不胜感激

【问题讨论】:

    标签: twitter-bootstrap ruby-on-rails-4 paperclip cover


    【解决方案1】:

    您必须将 url 用引号括起来 url('#{@user.background_image}') 但我想看看您的 User 类。您正在将@user.background_image@user.background.url 混合使用——后者看起来像您应该使用的那个。但是使用调试器自己看看。

    此外,如果您在回形针附件中添加了样式,则可能需要在调用 url 时进行公投。 (比如:thumb:large 或者你有什么......)

    【讨论】:

    • .url 是回形针合成器,图像不是问题,我可以从我的资产中添加任何图像,回形针是问题,我的意思是路径(即 url,或者至少我是这么认为的)
    • 无论如何,我修复了使用引导程序的问题,我给了它一些属性以使其像背景一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    相关资源
    最近更新 更多