2015-12-11 31 views
0

我正在嘗試創建表單。它的一部分是在導軌4中使用紙夾寶石上傳圖像。當我上傳時,出現錯誤「圖像不能出錯」。回形針:圖像不能爲空錯誤

有人可以幫助我嗎?

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 %> 

回答

1

你沒有約束力形式的圖像屬性,檢查以下。

<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>