简体   繁体   English

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

[英]Ruby-On-Rails how to display image uploaded by user

I am using Carrierwave so that a user can upload an image. 我正在使用Carrierwave,以便用户可以上传图像。 I am making a blog website. 我正在建立一个博客网站。 I have no errors but the image is not appearing. 我没有错误,但是图像没有出现。 I want the image to appear in the post VIEW. 我希望图像显示在帖子VIEW中。 Here is my code: 这是我的代码:

Post.rb Post.rb

mount_uploader :image, ImageUploader

Posts _form.html.erb 帖子_form.html.erb

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

image_uploader.rb 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

Posts show.html.erb 帖子show.html.erb

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

When I run my application and view a post it just says Image: with no image underneath it. 当我运行我的应用程序并查看帖子时,它只显示Image:在其下面没有图像。

UPDATE: 更新:

I installed ImageMagick/GraphicsMagick, and when I go to save post it says: 我安装了ImageMagick / GraphicsMagick,当我去保存帖子时说:

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`

and underneath it displays the image but it wont save the post. 在其下方显示图片,但不会保存该帖子。

While form_for sets encoding multipart automatically, but in my projects I still define it. 虽然form_for会自动设置多部分编码,但是在我的项目中我仍然定义它。 Try this: 尝试这个:

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

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

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