Paperclip:Image 不能为空错误

Paperclip:Image can't be blank error

我正在尝试创建一个表单。其中一部分是在 rails 中使用回形针 gem 上传图像 4. 当我上传时出现错误 "Image can't be error".

有人可以帮我解决这个问题吗?

Product.rb 文件:

class Product < ActiveRecord::Base
    belongs_to :user
  belongs_to :category
    has_many :comments , dependent: :destroy
    has_attached_file :image ,:styles => {
  :thumb    => ['100x100#',  :jpg, :quality => 70],
  :preview  => ['480x480#',  :jpg, :quality => 70],
  :large    => ['600>',      :jpg, :quality => 70],
  :retina   => ['1200>',     :jpg, :quality => 30]
},
:convert_options => {
  :thumb    => '-set colorspace sRGB -strip',
  :preview  => '-set colorspace sRGB -strip',
  :large    => '-set colorspace sRGB -strip',
  :retina   => '-set colorspace sRGB -strip -sharpen 0x0.5'
}

    validates_attachment_presence :image
    validates_attachment_size :image , :less_than => 10.megabytes
    validates_attachment_content_type :image , :content_type => ['image/jpeg','image/jpg','image/png']
    validates :image, presence: true 
#   accepts_nested_attributes_for :product , :reject_if => lambda { |t| t['image'].nil? }
end

_form.html.erb 文件

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

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

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.number_field :price %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :reason %><br>
    <%= f.text_area :reason %>
  </div>
  <div>
    <%= f.label :status %>
    <%=  f.select :status, options_for_select([ "Not sold", "Sold","Booked" ], "Not sold") %>
  </div>
  <div class="field">
    <%= f.label :image %><br>
    <%= f.file_field "images", type: :file %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

您没有在表单中绑定图像属性,请检查下方。

<div class="field">
    <%= f.label :image %><br>
    <%= f.file_field "images", type: :file %>
</div>

<div class="field">
    <%= f.label :image %><br>
    <%= f.file_field :image%>
</div>