我正在尝试创建一个表单。其中一部分是使用导轨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 %>
答案 0 :(得分:1)
您没有绑定表单中的图像属性,请查看下面的内容。
onclick="Hide_Site_Overlay(); setTimeout(Show_Next_Overlay, 750);"
到
<div class="field">
<%= f.label :image %><br>
<%= f.file_field "images", type: :file %>
</div>