简体   繁体   English

如何在 rails (4.1.5) 中创建装置(对于设计用户)作为 yml.erb?

[英]How to create fixtures (for a Devise user) as a yml.erb in rails (4.1.5)?

Update 3 : It seems like this is specific to fixtures in a .yml.erb - even if I have no templated code, it seems like fixtures in a yml.erb file doesn't get loaded.更新 3 :这似乎特定于 .yml.erb 中的装置 - 即使我没有模板化代码,似乎 yml.erb 文件中的装置也没有被加载。 Having a plain .yml file works.拥有一个普通的 .yml 文件。 This likely has nothing to do with devise per se.这可能与设计本身无关。

Note: see Update 3 annotations for relevant changes注意:有关相关更改,请参阅更新 3 注释

I need to generate Devise users in my rails app.我需要在我的 rails 应用程序中生成设计用户。 I notice that clearing the database and loading the fixtures loads all other fixtures, except the Devise users ( Update 3 : which is in a .yml.erb file).我注意到清除数据库并加载设备会加载所有其他设备,除了设计用户(更新 3 :在 .yml.erb 文件中)。

I have seen this other thread , but I tried all the options there and still doesn't seem to load the fixtures.我看过另一个线程,但我尝试了那里的所有选项,但似乎仍然没有加载夹具。

# ../fixtures/users.yml.erb
user1:
  email: user1@mysite.com
  name: user1
  encrypted_password: <%= Devise.bcrypt(User, 'passw0rd!') %>
  # also tried encrypted_password: User.new(password_salt: '$2a$10$PoBe1MvkoGJsjMVTEjKqge').send(:password_digest, 'somepassword')
  admin: true

And from the console:从控制台:

To clear the test db:清除测试数据库:

$ bundle exec rake db:schema:load RAILS_ENV=test

To load the fixtures into test db:将夹具加载到测试数据库中:

$ bundle exec rake db:fixtures:load RAILS_ENV=test

Run rails console in test (no users found, but other model fixtures, like App, are being loaded):在测试中运行 rails 控制台(未找到用户,但正在加载其他模型装置,如 App):

$ rails c test
Loading test environment (Rails 4.1.5)
irb(main):001:0> User.first
  User Load (0.1ms)  SELECT  "users".* FROM "users"   ORDER BY "users"."id" ASC LIMIT 1
=> nil
irb(main):002:0> App.first
  App Load (0.1ms)  SELECT  "apps".* FROM "apps"   ORDER BY "apps"."id" ASC LIMIT 1
=> #<App id: 953336129,...>

Update 1 : Also tried passing in encrypted password generated from console, still no user records are found:更新 1 :还尝试传入从控制台生成的加密密码,仍然没有找到用户记录:

admin:
  email: user1@mysite.com
  name: user1
  encrypted_password: $2a$04$DR0.2yfWwD8AZlyeXx0gEuk2Qh.cNLF4cir0ZUB1iW7hwQhK/IfcC
  admin: true

Update 2 : It works when I rename fixtures file to users.yml.更新 2 :当我将装置文件重命名为 users.yml 时它起作用。 Renaming to users.yml.erb seems to be the culprit.重命名为 users.yml.erb 似乎是罪魁祸首。 BTW, the same behavior is seen (that is, it works with .yml, but not with yml.erb) on the console and from rake test顺便说一句,在控制台和rake test可以看到相同的行为(即,它适用于 .yml,但不适用于 yml.erb)

You should pass the password in plain text too.您也应该以纯文本形式传递密码。 I am sure there is a User model validation errors preventing your fixture users from being created.我确信存在用户模型验证错误,阻止了您的装置用户的创建。 Here's an example from my users fixture which works:这是我的用户装置中的一个示例,该示例有效:

tom:
  first_name: Tom
  last_name: Test
  email: test@example.org
  password: 123greetings
  encrypted_password: <%= User.new.send(:password_digest, '123greetings') %>

If it still fails, please check the log/test.log file for errors and check for missing required fields or other validation rules you set in your User model.如果仍然失败,请检查log/test.log文件是否有错误,并检查是否缺少您在 User 模型中设置的必填字段或其他验证规则。

Update: It turns out that author found the issue himself - used .yml.erb file extension rather than .yml which made rails bypass that fixtures file.更新:原来是作者自己发现了这个问题——使用了 .yml.erb 文件扩展名而不是 .yml ,这使得 rails 绕过了该装置文件。 ERB works in yml fixtures as rails runs the fixture file through ERB before parsing it. ERB 在 yml 夹具中工作,因为 rails 在解析它之前通过 ERB 运行夹具文件。

<% 100.times do |n| %>
user_<%= n %>:
  email: <%= "user#{n}@example.com" %>
  encrypted_password: <%= Devise.bcrypt(User, 'password') %>
<% end %>

It's an alternative way to do the same thing, you don't have to new an instance.这是做同样事情的另一种方式,您不必新建一个实例。

# lib/devise/models/database_authenticatable.rb:147
def password_digest(password)
  Devise.bcrypt(self.class, password)
end

edit:编辑:

Thank @sixty4bit for pointing out deprecated usage, the updated answer would be:感谢@sixty4bit 指出已弃用的用法,更新后的答案是:

<% 100.times do |n| %>
user_<%= n %>:
  email: <%= "user#{n}@example.com" %>
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
<% end %>

Since all of these answers were fairly confusing or out of date, the newest answer is that you need to use Devise::Encryptor.digest to create the encrypted_password.由于所有这些答案都相当混乱或过时,因此最新的答案是您需要使用Devise::Encryptor.digest来创建 encrypted_pa​​ssword。

Example:例子:

# ../fixtures/users.yml.erb
user1:
  email: user1@mysite.com
  name: user1
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
  admin: true

user2:
  email: user2@mysite.com
  name: user2
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>
  admin: false

Hope that helps!希望有帮助!

admin:
 id: 1
 first_name: admin
 last_name: tukaweb
 email: 'admin@email.com'
 encrypted_password: <%= User.new.send(:password_digest, 'admin@123') %>
 phone: 9541555660

regular:
 id: 2
 first_name: vidur
 last_name: punj
 email: 'vidur.punj@hotmail.com'
 encrypted_password: <%= User.new.send(:password_digest, 'punj@123') %>
 phone: 9541555660

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM