Ruby-on-rails 未设置设计密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18080910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:29:19  来源:igfitidea点击:

Devise Secret Key was not set

ruby-on-railsdevise

提问by

I am developing a Rails 4 app using the Active Admin gem for the administration back end. Active Admin in turn uses Devise for user authentication. Now, when I try to deploy the app using capistranoon the VPS server, I get the below error:

我正在使用 Active Admin gem 为管理后端开发 Rails 4 应用程序。Active Admin 反过来使用 Devise 进行用户身份验证。现在,当我尝试capistrano在 VPS 服务器上部署应用程序时,出现以下错误:

rake aborted!
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = '-- secret key --'

A Google search does not do much for this error. Any suggestions why it is throwing an error? Should I add the secret key to deviseinitializer, as I cannot find any place to set such config key in initializers/devise.rb?

谷歌搜索对这个错误没有多大作用。任何建议为什么会抛出错误?我是否应该将密钥添加到devise初始化程序,因为我找不到任何地方可以设置这样的配置密钥initializers/devise.rb

采纳答案by Brian Weiner

I ran bundle updatethis morning and started getting the same error.

bundle update今天早上跑了,开始遇到同样的错误。

I added it as a line in config/initializers/devise.rband the error was fixed.

我将它添加为一行config/initializers/devise.rb并修复了错误。

This seems to be the commitwhich introduced it.

这似乎是引入它的提交

回答by Paul Odeon

What worked for me on Rails 4.1 and Devise 3.2.4 is in config/initializers/devise.rb:

在 Rails 4.1 和 Devise 3.2.4 上对我有用的是config/initializers/devise.rb

config.secret_key = ENV['DEVISE_SECRET_KEY'] if Rails.env.production?

回答by Brandon Cook

As of Devise 3.2.3for Rails 4+ applications the key setting location defaults to YourAppName::Application.config.secret_key_basefound in config/initializers/secret_token.rb

作为设计3.2.3为Rails 4+应用的关键位置设置默认为YourAppName :: Application.config.secret_key_base发现,在配置/初始化/ secret_token.rb

回答by Jean-Nicholas Hould

This solved my problem:

这解决了我的问题:

Add the code below to your config/initializers/devise.rbfile.

将下面的代码添加到您的config/initializers/devise.rb文件中。

config.secret_key = '-- secret key --' 

Replace '-- secret key--' with your own key. I recommend storing it in an ENV variable for security purpose.

将“--密钥--”替换为您自己的密钥。为了安全起见,我建议将其存储在 ENV 变量中。

回答by Eric

As per changelog:

根据变更日志

Devise will use the secret_key_base on Rails 4+ applications as its secret_key. You can change this and use your own secret by changing the devise.rb initializer.

Devise 将在 Rails 4+ 应用程序上使用 secret_key_base 作为其 secret_key。您可以通过更改 devise.rb 初始值设定项来更改此设置并使用您自己的秘密。

I went to config/secrets.ymland changed the productionvalue.

我去config/secrets.yml改变了production价值。

Before:

前:

production: 
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

After:

后:

production: 
  secret_key_base: string of charaters

Of course, that should be set to the environment variable, which I will set later, but this at least got it running. I got my string by using bundle exec rake secret.

当然,这应该设置为环境变量,我稍后会设置,但这至少让它运行了。我通过使用得到了我的字符串bundle exec rake secret

回答by rld

In config/initializers/devise.rbI put:

config/initializers/devise.rb我提出:

config.secret_key = ENV["SECRET_KEY_BASE"] if Rails.env.production?

Because if you put:

因为如果你把:

$ heroku config

You'll see a secret_key_basefor the mode production.

您会看到secret_key_basemode production

回答by sascha.daniels

Could it be, that you did not run rails g devise:install?

难道,你没有跑rails g devise:install

Running rails generate devise Userwithout the previous command does cause this problem.

rails generate devise User没有上一个命令的情况下运行确实会导致此问题。

回答by Andrey Yasinishyn

I solve my initializer problem with this ugly approach:

我用这种丑陋的方法解决了我的初始化问题:

config.secret_key = 'some1234keyq23' if Rails.env == 'production'

in config/initializers/devise.rb It now works in production as well as in development !

在 config/initializers/devise.rb 它现在可以在生产和开发中使用!

回答by jgrumps

I cloned my repository onto a new machine from git. The

我从 git 将我的存储库克隆到一台新机器上。这

config/secrets.yml 

file was on my .gitignore list, so that file didn't exist, and Devise doesn't create the file.

文件在我的 .gitignore 列表中,因此该文件不存在,并且 Devise 不会创建该文件。

I added the file, then re-ran

我添加了文件,然后重新运行

rails generate devise MODEL

and it worked.

它奏效了。

回答by zurbergram

Check if your config\initializers\secret_token.rbhas:

检查您config\initializers\secret_token.rb是否有:

YourAppName::Application.config.secret_token

It should be:

它应该是:

YourAppName::Application.config.secret_key_base