我知道用户存储在一个名为users的表中,但底层数据库是MySQL,我不知道如何访问Ruby on Rails应用程序的数据库.
有谁知道如何添加用户?
这是一个非常“经典”的问题,直接思考(“只做它mySQL”)在这里不起作用,因为需要有一个编码输入密码的rails片段. 所以你需要实际使用rails,就像这样(这应该在本地开发环境中发生,这是本地工作时的默认设置):您需要创建一个用户.
试试这个:
cd the_root_of_the_project
script/rails console > User.create(:username => 'admin', :password => 'abc123', :password_confirmation => 'abc123') # Add other fields, such as first_name, last_name, etc. # as required by the user model validators. # Perhaps :admin => true
这假定了一些事情(根据需要进行更改),例如身份验证系统(如authLogic或设计,属性和字段名称等),但您应该能够根据自己的需要进行调整.您可以通过查看一些内容来确定这些内容,特别是db / migrate中的数据库迁移文件,user / model / user中的模型验证,db / seeds.rb中用户的任何现有“种子”文件以及身份验证系统挂钩.
关于“在哪里”这样做 – 显然控制台可以工作,但你可能也想使用种子文件.无论你在控制台中使用什么’create’命令都可以放在这里,然后使用rake db:seed运行.缺点是,如果您将此文件检入源代码管理,则其安全性较低.种子文件对于其他任务非常有用,例如创建参考表,初始类别等.
如果您还没有实际创建数据库,则需要了解并使用这些任务:
rake db:create # as it sounds, creates a database (but no application tables or columns), # using the config/database.yml file for the connection info. rake db:migrate # Creates tables and columns using the db/migrate/ files. rake db:seed # Runs commands in db/seeds.rb to create initial records for the application.
精彩评论