Google Map的堆栈级别太深错误

问题描述

我一直在得到这个堆栈级别太深的错误,我不确定它从哪里来。这是错误

error page

我正在使用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);

});

我相当确定错误是由模型引起的,但我对所有内容仍然有些陌生。任何帮助将不胜感激! :)

解决方法

原来是“ to_json”方法引起了问题。

在我的情况下,此问题的根本原因是模型中的字段与附件具有相同的名称(表中的列),这是不需要的,因为Active Storage使用单独的表。当在这样的模型对象上调用to_json时,会导致堆栈级别太深的错误。从数据库中删除该列后,问题就消失了。

This post really helped my problem

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...