Search code examples
ruby-on-railsrubybackendserver-side

systemStackError using ruby on rails


i created a little project using ruby on rails 5. I want to create an index page for show Thesis records. So i wrote:

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

this method should show thesis records like html and like json, but while like html it works, when i ask localhost:3000/theses.json i received from puma server this error:

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'

Do you have any idea about this error?

This is my Thesis model:

class Thesis < ApplicationRecord
  has_many_attached :img  
end

Thanks.


Solution

  • In your model is a wrong attribute: has_many means that there can be many images, but the parameter is 'img' that means there can be only one image. This error belongs to ActiveStorage and raised when attached reference name is the same as the model attribute.

    Use next to solve this error:

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