我正在尝试从我使用 exifr gem 用回形针上传的 jpeg 图像中读取 exif 数据,并将焦距等保存到属性中,但我似乎无法做到。我是一名 Rails 初学者,如果有人能提供帮助,我将不胜感激。
我的 photo.rb 模型:
class Photo < ActiveRecord::Base
attr_accessible :date, :exif, :name, :photo
has_attached_file :photo, :styles => { :small => "200x200#" , :medium => "1280x720#" },
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"
after_photo_post_process :load_exif
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg']
def load_exif
exif = EXIFR::JPEG.new(photo.queued_for_write[:original])
return if exif.nil? or not exif.exif?
self.exposure = exif.exposure_time.to_s
self.f_stop = exif.f_number.to_f.to_s
self.focal_length = exif.focal_length.to_f.round.to_s
self.iso = exif.iso_speed_ratings
self.date = exif.date_time.to_date
rescue
false
end
end
我的表格(使用 SimpleForm):
<%= simple_form_for @photo, :html => {:multipart => true} do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.file_field :photo, :label => 'Attach photo' %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
show.html.erb 带有我要显示的字段:
<p>
<b>Name:</b>
<%= @photo.name %>
</p>
<p>
<b>Date:</b>
<%= @photo.date %>
</p>
<p>
<b>Exposure:</b>
<%= @photo.exposure %>
</p>
我可能错过了一些愚蠢的东西,所以如果是这样,我很抱歉。