pc-orion

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Trying to create category, Getting SystemStackError (stack level too deep) in rails 6
I'm encountering a SystemStackError in my Ruby on Rails application, specifically in one of my controllers. I've been trying to debug it, but I'm having trouble pinpointing the exact cause. Here's the error message and the relevant part of my controller:

server logs

Started POST "/api/v1/categories" for ::1 at 2023-09-07 00:30:36 +0530
Processing by Api::V1::CategoriesController#create as JSON
Parameters: {"name"=>"Electronics", "category"=>{"name"=>"Electronics"}}
Can't verify CSRF token authenticity.
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."token" IS NULL LIMIT $1 [["LIMIT", 1]]
↳ app/controllers/application_controller.rb:13:in authenticate_request'
TRANSACTION (0.9ms) BEGIN
↳ app/controllers/application_controller.rb:15:in
authenticate_request'
User Update (1.1ms) UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "updated_at" = $3 WHERE "users"."id" = $4 [["sign_in_count", 2], ["current_sign_in_at", "2023-09-06 19:00:36.703908"], ["updated_at", "2023-09-06 19:00:36.704756"], ["id", 1]]
↳ app/controllers/application_controller.rb:15:in authenticate_request'
TRANSACTION (1.4ms) COMMIT
↳ app/controllers/application_controller.rb:15:in
authenticate_request'
TRANSACTION (0.8ms) BEGIN
↳ app/controllers/api/v1/categories_controller.rb:17:in create'
Category Create (1.1ms) INSERT INTO "categories" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["name", "Electronics"], ["created_at", "2023-09-06 19:00:36.729039"], ["updated_at", "2023-09-06 19:00:36.729039"]]
↳ app/controllers/api/v1/categories_controller.rb:17:in
create'
TRANSACTION (0.9ms) COMMIT
↳ app/controllers/api/v1/categories_controller.rb:17:in create'
ActiveStorage::Attachment Load (1.8ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 2], ["record_type", "Category"], ["name", "image"], ["LIMIT", 1]]
↳ app/controllers/api/v1/categories_controller.rb:18:in
create'
Completed 500 Internal Server Error in 85ms (ActiveRecord: 21.6ms | Allocations: 40064)
SystemStackError (stack level too deep):

app/controllers/api/v1/categories_controller.rb:18:in `create'

**SystemStackError (stack level too deep):

app/controllers/api/v1/categories_controller.rb:7:in `index' ** And here's the code for the index action in api/v1/categories_controller.rb:

class Api::V1::CategoriesController < ApplicationController
before_action :set_category, only: [:show, :update, :destroy]

  def index
    @categories = Category.all
    # render json: @categories
  end

  def show
    render json: @category
  end

  def create
    @category = Category.new(category_params)

    if @category.save
      render json: @category, status: :created
    else
      render json: @category.errors, status: :unprocessable_entity
    end
  end

  def update
    if @category.update(category_params)
      render json: @category
    else
      render json: @category.errors, status: :unprocessable_entity
    end
  end

  def destroy
    @category.destroy
    # head :no_content
  end

  private

  def set_category
    @category = Category.find(params[:id])
  end

  def category_params
    params.require(:category).permit(:name, :image)
  end

end

class ApplicationController < ActionController::Base
include JsonWebToken
protect_from_forgery with: :null_session

before_action :authenticate_request
before_action :configure_permitted_parameters, if: :devise_controller?
attr_reader :current_user

private

def authenticate_request
token = request.headers['Authorization']&.split(' ')&.last
user = User.find_by(token: token)
if user.present?
sign_in user, store: false
else
render json: { error: 'Invalid token' }, status: :unauthorized
end
end

protected

def configure_permitted_parameters
added_attrs = [:mobile_number, :email, :first_name, :last_name]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
end

end
I've checked the code, and it doesn't seem to contain any obvious recursive function calls. What could be causing this SystemStackError in this context?

I'd appreciate any guidance on how to diagnose and resolve this issue. Thank you!
https://stackoverflow.com/q/77054743/22404949