Rails SystemStackError (stack level too deep) in actionpack



今天我的Ruby on Rails API出现了错误,我无法弄清楚问题是什么。每当我尝试在我的控制器中创建一个新角色时,我总是得到一个SystemStackError,我不知道为什么。我将在下面发布相关文件。

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

路线.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

字符.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

这个问题的答案是将字符的属性重命名为类。这以某种方式导致了错误,更改后问题消失了。感谢所有帮助我解决这个问题的人。

相关内容

  • 没有找到相关文章

最新更新