[英]Rails SystemStackError (stack level too deep) in actionpack
today I got to an error with my Ruby on Rails API and I can't figure out what the problem is. 今天,我在Ruby on Rails API上遇到了一个错误,但我不知道问题出在哪里。 Whenever I try to create a new Character in my Controller I always get an SystemStackError and I have no Idea why.
每当我尝试在Controller中创建新角色时,总是会收到SystemStackError,我也不知道为什么。 I will post the relevant Files below.
我将在下面发布相关文件。
characters_controller.rb character_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 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) character.rb(我的模型)
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: 第11行是:
@newchar = Character.new
The answer to this problem was to rename the attribute of Character named class. 解决此问题的方法是重命名Character命名类的属性。 This has somehow caused the error and after changing it the problem was gone.
这以某种方式导致了错误,并且在更改它后问题仍然消失了。 Thanks to everyone who helped me with this problem.
感谢所有帮助我解决此问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.