ruby on rails:<top(required)=“”>':超类必须是一个类(给定符号)(TypeError)

时间:2017-01-08 17:52:33

标签: ruby-on-rails ruby ruby-on-rails-4

在我的rails服务器中,当我输入'rails console production'并且我不知道为什么时会发生此错误。 我要做的是生成用户信息并将其保存在我的数据库中。 我猜有一些json或符号错误,但我无法解决... 任何帮助将不胜感激。

这是我的错误消息。

user_controller.rb:17:in `<top (required)>': superclass must be a Class (Symbol given) (TypeError)
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:360:in `require_or_load'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:317:in `depend_on'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:233:in `require_dependency'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/engine.rb:471:in `each'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/engine.rb:471:in `block in eager_load!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/engine.rb:469:in `each'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/engine.rb:469:in `eager_load!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/engine.rb:346:in `eager_load!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/application/finisher.rb:56:in `each'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/application/finisher.rb:56:in `block in <module:Finisher>'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:345:in `each'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:345:in `call'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
from /home/ec2-user/.rvm/rubies/ruby-2.2.5/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
from /home/ec2-user/popcake/config/environment.rb:5:in `<top (required)>'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/application.rb:328:in `require'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/application.rb:328:in `require_environment!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:142:in `require_application_and_environment!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:67:in `console'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `require'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `block in exec_app_rails'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `loop'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `exec_app_rails'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/lib/rails/cli.rb:5:in `<top (required)>'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/bin/rails:9:in `require'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/gems/railties-4.2.5/bin/rails:9:in `<top (required)>'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/bin/rails:23:in `load'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/bin/rails:23:in `<main>'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `eval'
from /home/ec2-user/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `<main>'

这是我的user_controller

require 'openssl'
require 'jwt'
require 'json'

class UserController < 
    def register_native
        userparams=params.require(:user).permit(:uuid, :username, :email, :encrypted_password)
        expire = Time.now.to_i + 4 * 3600
        rsa_private = OpenSSL::PKey::RSA.generate 2048
        rsa_public = rsa_private.public_key
        token = JWT.encode userparams, rsa_private, 'RS256'
        jsonparams ={:uuid => userparams[:uuid],:username => userparams[:username],:email => userparams[:email],:encrypted_password => userparams[:encrypted_password],:private_key =>rsa_private,:token =>token, :exp => expire}.to_json
        responses={:public_key=>rsa_public, :token=>token}.to_json
        User.create(jsonparams)
        render json: responses
    end

2 个答案:

答案 0 :(得分:6)

试试这个

class UserController < ApplicationController

为什么你的代码不起作用?

# bad code
class UserController < 
  def register_native
  end
end

Ruby中的所有内容都是一个声明,

  • class是一个带有三个参数的语句:class name,superclass object和body
  • def是一个创建方法并将名称作为符号返回的语句

因此,您的代码缺少一个超类参数,因此第一个方法定义将执行,其返回值将用作超类参数。

在伪代码中,这看起来像

# pseudocode 
define_class(
  "UserController", 
  define_method("register_native", &empty_block),
  &empty_block
)

那会发生什么?

  • def在全局范围内创建方法并返回符号:register_native
  • 然后将此符号作为超类参数
  • 传递给class语句
  • 引发错误"superclass must be a Class (Symbol given)"

有趣的是,任何返回类的语句都对超类表达式

有效
# Fun fact, this is valid Ruby
class Surprise < [String, Array, Fixnum, Hash].sample
end

答案 1 :(得分:1)

user_controller.rb文件更改中:

class UserController <

class UserController < ApplicationController

发生此错误是因为您的class未从superclass继承。