【发布时间】:2020-10-01 15:21:22
【问题描述】:
我得到这个堆栈级别太深的错误,我不确定它来自哪里。这是错误:
我正在使用 google maps javascript api 来显示项目。这是模型:
class Item < ApplicationRecord
belongs_to :user
has_one_attached :photo
validates :price, presence: true, if: :available_for_rent?
#address validations
validates :street, presence: true
validates :city, presence: true
validates :state, presence: true
validates :zip, presence: true
scope :rentable, -> { where(available_for_rent: true) }
#google maps
geocoded_by :address
after_validation :geocode
def address
[street, city, zip, state].compact.join(", ")
end
#validate size of photo uploads
validate :photo_validation
def photo_validation
if photo.attached?
if photo.blob.byte_size > 3000000
photo.purge
errors[:base] << 'The file uploaded was too big. Jubal Talents only allows photo uploads under 3 MB.'
elsif !photo.blob.content_type.starts_with?('image/')
photo.purge
errors[:base] << 'Wrong format. Please ensure file is jpg or png.'
end
end
end
end
这里是触发错误的html:
<div class="column">
<%= tag.div nil, id:'map', data: {items: @items.to_json(methods: [:address])}%>
<div class="notification">
<p class="title is-6">Addresses are not exact. You must contact the owner to recieve a pickup address.</p>
</div>
</div>
</div>
这里是javascript:
document.addEventListener("turbolinks:load", function() {
var map = new GMaps({
div: '#map',
lat: 38.5816,
lng: -121.4944
});
window.map = map;
var items = JSON.parse(document.querySelector("#map").dataset.items);
window.items = items;
var bounds = new google.maps.LatLngBounds();
items.forEach(function(item) {
if (item.latitude && item.longitude) {
var marker = map.addMarker({
lat: (item.latitude - Math.random()*.1),
lng: (item.longitude - Math.random()*.1),
title: item.address,
infoWindow: {
content: `<p><a href="/items/${item.id}">${item.name}</a></p>`
}
})
bounds.extend(marker.position);
}
});
map.fitBounds(bounds);
});
我相当确定错误来自模型,但我对所有事情仍然有些陌生。任何帮助将不胜感激! :)
【问题讨论】:
-
您可以使用一些试错法来查明错误。注释掉各种代码(例如,暂时将 address 方法替换为返回假字符串的方法)并查看哪些更改可以消除错误。
-
感谢您的最大回答!我意识到它是什么
标签: ruby-on-rails ruby ruby-on-rails-5.2