Rails actionpack 中的 SystemStackError(堆栈级别太深)
Rails SystemStackError (stack level too deep) in actionpack
今天我的 Ruby 在 Rails API 上出错,我无法弄清楚问题出在哪里。每当我尝试在我的控制器中创建一个新角色时,我总是会得到一个 SystemStackError 并且我不知道为什么。我将 post 下面的相关文件。
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(我的模型)
class Character < ActiveRecord::Base
belongs_to :user
end
我得到的错误(堆栈跟踪)是:
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)
感谢任何帮助,因为我不知道如何处理这个问题:)谢谢
编辑:每当我尝试再次发送相同的请求时,我都会收到不同的错误:
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)
第 11 行是:
@newchar = Character.new
这个问题的答案是重命名名为 class 的 Character 的属性。这不知何故导致了错误,并且在更改它之后问题就消失了。感谢所有帮助我解决这个问题的人。
今天我的 Ruby 在 Rails API 上出错,我无法弄清楚问题出在哪里。每当我尝试在我的控制器中创建一个新角色时,我总是会得到一个 SystemStackError 并且我不知道为什么。我将 post 下面的相关文件。
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(我的模型)
class Character < ActiveRecord::Base
belongs_to :user
end
我得到的错误(堆栈跟踪)是:
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)
感谢任何帮助,因为我不知道如何处理这个问题:)谢谢
编辑:每当我尝试再次发送相同的请求时,我都会收到不同的错误:
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)
第 11 行是:
@newchar = Character.new
这个问题的答案是重命名名为 class 的 Character 的属性。这不知何故导致了错误,并且在更改它之后问题就消失了。感谢所有帮助我解决这个问题的人。