【问题标题】:Not Displaying Image with Carrierwave不使用载波显示图像
【发布时间】:2013-05-01 06:25:12
【问题描述】:

我一直在寻找一种将照片上传到数据库并在页面上显示照片的方法。我正在尝试载波。我认为它可以正确上传照片,但是当它显示图像时,它所做的只是给出它的位置(url)。

这里是上传者

class Image < ActiveRecord::Base
attr_accessible :image, :title
  mount_uploader :image, ImageUploader
end

这是用于上传文件的表单

<%= form_for @image, :html => {:multipart => true} do |f| %>
  <% if @image.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2>

      <ul>
      <% @image.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :image %><br />
    <%= f.file_field :image %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是为了显示图片

<p id="notice"><%= notice %></p>

<p>
  <b>Image:</b>
  <%= @image.image_url.to_s %>
</p>

<p>
  <b>Title:</b>
  <%= @image.title %>
</p>


<%= link_to 'Back', images_path %>

这是打印出来的:

图片:/uploads/image/image/2/946612_524939910875157_1455921715_n.jpg

标题:海报

【问题讨论】:

  • 将&lt;%= @image.image_url.to_s %&gt;更改为&lt;%= image_tag @image.image_url.to_s %&gt;

标签: ruby-on-rails carrierwave


【解决方案1】:

要显示这张图片,试试这个。

<%= image_tag @image.image_url.to_s if @image.image_url.present? %>

【讨论】:

  • 你可以直接使用@image.image_url,不需要"#{@image.image_url}"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 2014-05-21
  • 2015-06-08
  • 1970-01-01
相关资源
最近更新 更多