在 rails 上使用 ruby 的 systemStackError

systemStackError using ruby on rails

我在 rails 上使用 ruby 创建了一个小项目 5. 我想为展示论文记录创建一个索引页面。所以我写道:

def index
    respond_to do |format|
      format.html 
      format.json {
       render :json => Thesis.all
      }
    end
end

此方法应该显示像 html 和 json 这样的论文记录,但是虽然像 html 它有效,但当我问 localhost:3000/theses.json我从 Puma 服务器收到此错误:

SystemStackError (stack level too deep):

app/controllers/theses_controller.rb:12:in `block (2 levels) in index'
app/controllers/theses_controller.rb:9:in `index'

你知道这个错误吗?

这是我的论文模型:

class Thesis < ApplicationRecord
  has_many_attached :img  
end

谢谢。

在你的模型中有一个错误的属性:has_many表示可以有很多张图片,但是参数是'img'表示只能有一张图片。此错误属于 ActiveStorage,并在附加的引用名称与模型属性相同时引发。

使用下一步解决这个错误:

has_many_attached :imgs #for multiple images
has_one_attached :img #for one image per object