Ruby-On-Rails如何显示用户上传的图片

Ruby-On-Rails how to display image uploaded by user

我正在使用 Carrierwave,以便用户可以上传图像。我正在制作一个博客网站。我没有错误,但图像没有出现。我希望图像显示在 post 视图中。这是我的代码:

Post.rb

mount_uploader :image, ImageUploader

帖子_form.html.erb

  <div>
    <%= form_for @post do |f|%>
    <%= f.label :image %>
    <%= f.file_field :image %>
  </div>

image_uploader.rb

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

帖子show.html.erb

<strong>Image:</strong>
    <%= image_tag @post.image_url%>
</p>

当我 运行 我的应用程序并查看 post 它只是说图像:下面没有图像。

更新:

我安装了 ImageMagick/GraphicsMagick,当我去保存 post 时它说:

1 error prohibited this post from being saved:
Image Failed to manipulate with MiniMagick, maybe it is not an image? `Original Error: ImageMagick/GraphicsMagick is not installed`

在其下方显示图像但不会保存 post。

虽然form_for自动设置编码多部分,但在我的项目中我仍然定义它。试试这个:

<%= form_for(@post, hmtl: { multipart: true } ) do |f| %>