today I got to an error with my Ruby on Rails API and I can't figure out what the problem is. Whenever I try to create a new Character in my Controller I always get an SystemStackError and I have no Idea why. I will post the relevant Files below.
characters_controller.rb
module Api
module V1
class CharactersController < ApplicationController
def index
@chars = Character.all
respond_to do |format|
format.json { render :json => @chars }
end
end
def create
@newchar = Character.new
lastmod = params[:lastModified]
charname = params[:name]
realm = params[:realm]
battlegroup = params[:battlegroup]
charclass = params[:class]
race = params[:race]
gender = params[:gender]
level = params[:level]
achievement_points = params[:achievementPoints]
thumbnailurl = params[:thumbnailurl]
itemlvltotal = params[:itemleveltotal]
itemlvlequipped = params[:itemlevelequipped]
userid = params[:userid]
@newchar.lastModified = lastmod
@newchar.name = charname
@newchar.realm = realm
@newchar.battlegroup = battlegroup
@newchar.class = charclass
@newchar.race = race
@newchar.gender = gender
@newchar.level = level
@newchar.achievementPoints = achievement_points
@newchar.thumbnailurl = thumbnailurl
@newchar.itemleveltotal = itemlvltotal
@newchar.itemlevelequipped = itemlvlequipped
@newchar.userid = userid
if @newchar.save!
render json: {status: 'success', code: 0, message: 'Character has been saved'}
else
render json: {status: 'error', code: 1, message: 'Failed to save character'}
end
end
def update
end
def mychars
@chars = Character.where(userid: params[:userid])
render json: {characters: @chars}
end
end
end
end
routes.rb
Rails.application.routes.draw do
namespace :api, :defaults => {:format => :json} do
namespace :v1 do
resources :users do
collection do
post 'register'
post 'login'
get 'make_moderator'
end
end
resources :raids do
collection do
post 'create'
get 'details'
post 'sign_up'
post 'sign_off'
post 'signedup'
end
end
resources :characters do
collection do
get 'mychars'
post 'create'
post 'update'
end
end
end
end
end
character.rb (my model)
class Character < ActiveRecord::Base
belongs_to :user
end
The error (stacktrace) I get is:
Started POST "/api/v1/characters/create" for 82.112.107.65 at 2015-01-07 17:30:10 +0100
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by Api::V1::CharactersController#create as JSON
Parameters: {"lastModified"=>"1194551616", "name"=>"Mortan", "realm"=>"Kargath", "battlegroup"=>"Reckoning / Abrechnung", "class"=>"5", "race"=>"4", "gender"=>"0", "level"=>"72", "achievementPoints"=>"7245", "thumbnailurl"=>"http://eu.battle.net/static-render/eu/kargath/240/386800-avatar.jpg", "itemleveltotal"=>"137", "itemlevelequipped"=>"127", "userid"=>"10"}
Completed 500 Internal Server Error in 26ms
SystemStackError (stack level too deep):
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:79
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack- 4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.8ms)
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms)
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.0ms)
Any help is appreciated since I don't know how to handle this problem :) thanks
EDIT: Whenever I try to send the same request again I get a different error:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/api/v1/characters_controller.rb:11:in `create'
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.2ms)
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.2ms)
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms)
Rendered /usr/local/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (28.3ms)
Where line 11 is:
@newchar = Character.new
The answer to this problem was to rename the attribute of Character named class. This has somehow caused the error and after changing it the problem was gone. Thanks to everyone who helped me with this problem.