[英]Rails: NameError (uninitialized constant UserSerializer)
I am currently building a Rails API-only application using Rails 6.0.我目前正在使用 Rails 6.0 构建一个仅限 Rails API 的应用程序。 I am using fast_jsonapi for JSON:API serialization of Ruby Objects.我正在使用fast_jsonapi进行 JSON:API 序列化 Ruby 对象。 For now I have created only the UsersController
.现在我只创建了UsersController
。
I have a user
model with the following attributes:我有一个具有以下属性的user
模型:
ActiveRecord::Schema.define(version: 2020_03_14_175719) do
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "password_digest"
t.string "email"
t.string "phone"
t.text "address"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
end
I have created a user_serializer
for it:我为它创建了一个user_serializer
:
class UserSerializer
include FastJsonapi::ObjectSerializer
attributes :first_name, :last_name, :username, :password, :email, :phone, :address
end
I have also created a User
on the database via the rails console
with the following information:我还通过rails console
在数据库上创建了一个User
,其中包含以下信息:
User.delete_all
user = User.create! first_name: 'Promise', last_name: 'My last name', username: 'myusername', email: 'myemail@gmail.com', password: 'mypassword', phone: '002037363', address: 'my address'
puts "Created a new user: #{user.first_name} #{user.last_name} with email - #{user.email}"
However, when I run the command below in the rails console:但是,当我在 rails 控制台中运行以下命令时:
UserSerializer.new(User.first).serializable_hash
I get the error below:我收到以下错误:
NameError (uninitialized constant UserSerializer) NameError(未初始化的常量 UserSerializer)
I have tried a few solutions, but none seems to be working.我尝试了一些解决方案,但似乎都没有奏效。 I need some help.我需要帮助。 Thanks.谢谢。
Here's how I solved it :这是我解决它的方法:
Exit the rails console, if you're still inside it:退出 rails 控制台,如果你还在里面:
exit
Stop the spring Rails application preloader:停止spring Rails 应用程序预加载器:
spring stop
Enter rails console
in development environment again:再次进入开发环境中的rails console
:
rails console
Run the command again:再次运行命令:
UserSerializer.new(User.first).serializable_hash
It gives an output of this sort:它给出了这样的输出:
#<ActiveRecord::Relation [#<User id: 3, first_name: "Promise", last_name: "My last name", username: "myusername", password_digest: [FILTERED], email: "myemail@gmail.com", phone: "002037363", address: "my address", created_at: "2020-03-14 18:36:31", updated_at: "2020-03-14 18:36:31">]>
That's all.就这样。
I hope this helps我希望这有帮助
Reload the terminal source or starting a new terminal tab will also work.重新加载终端源或启动新的终端选项卡也将起作用。 Opening a new terminal and starting a new rails console
worked for me.打开一个新终端并启动一个新的rails console
对我有用。
Try reload!
reload!
试试reload!
in rails console.在导轨控制台中。
You can also open rails console
in another tab
or terminal
.您还可以在另一个tab
或terminal
打开rails console
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.