0

私はRuby on Railsが初めてで、まだメソッドを書くことができません....

exif データを含まない画像をアップロードすると、エラーが発生します。

imgfile と imgfile.nil の両方を許可するには、このメソッドが必要です。imfile.nil の場合、デフォルト値を定義するか、このコードを無視するにはどうすればよいですか?

# Image geolocation
def get_image_loc
imgfile = EXIFR::JPEG.new(image.queued_for_write[:original].path)
return unless imgfile

lat = imgfile.exif[0].gps_latitude[0].to_f + (imgfile.exif[0].gps_latitude[1].to_f / 60) + (imgfile.exif[0].gps_latitude[2].to_f / 3600)
lng = imgfile.exif[0].gps_longitude[0].to_f + (imgfile.exif[0].gps_longitude[1].to_f / 60) + (imgfile.exif[0].gps_longitude[2].to_f / 3600)

lat = lat * -1 if imgfile.exif[0].gps_latitude_ref == "S"      # (N is +, S is -)
lng = lng * -1 if imgfile.exif[0].gps_longitude_ref == "W"   # (W is -, E is +)

self.img_loc_lat  = lat # imgfile.gps_latitude
self.img_loc_lng  = lng # imgfile.gps_longitude
end

end

これが私のshow.erbファイルでこのメソッドを呼び出す部分です

<% unless @pin.img_loc_lat.blank?
            # bespoke code to load a map if the location information is available 
            # Google Static Maps for now
            # Look to upgrade to Google Maps API 3 - there seem to be a few gems available for it
        %>
            <p>&nbsp;<br />
                <img src="http://maps.googleapis.com/maps/api/staticmap?center=<%= @pin.img_loc_lat %>,<%= @pin.img_loc_lng %>&zoom=13&size=600x300&maptype=roadmap&markers=color:green%7Clabel:P%7C<%= @pin.img_loc_lat %>,<%= @pin.img_loc_lng %>&sensor=false">
            </p>
        <% end %>

私のギット

https://github.com/nathan-wallace/dcphotogrid-app.git

4

2 に答える 2

0

html ブロッ​​クをヘルパーとして書き直してから、.try() を使用してメソッドとして呼び出すことができます。

@instance.try(:method_name)

@instance が空白の場合、メソッドは呼び出されません。

于 2014-01-12T20:49:13.873 に答える
0

img_loc_lat が nil かどうかを確認できます。

<% unless @pin.img_loc_lat.nil? %>

文字列または nil である可能性がある場合はimg_loc_lat、文字列に変換して空かどうかを確認することもできます。

<% unless @pin.img_loc_lat.to_s.empty? %>
于 2013-04-27T22:06:50.217 に答える