10

I just started learning ruby on rails. I followed a lot of install examples, but when i ran the example am getting this error

A secret is required to generate an integrity hash for cookie session data. Use config.secret_token = "some secret phrase of at least 30 characters"in config/initializers/secret_token.rb

I search for it but i dont see too much help.

plz help.

Platform: Mac OS X.

Alyssa Ross
  • 2,169
  • 16
  • 26
Leonel Padilla
  • 101
  • 1
  • 1
  • 5

6 Answers6

34

The easiest way to generate a new secret token is to run

rake secret

at the command line.

allesklar
  • 9,506
  • 6
  • 36
  • 53
10

Your token should have been generated automatically by Rails, but you could still use something like:

irb(main):007:0> "#{ActiveSupport::SecureRandom.hex(64)}"
=> "921b00fcfabe0368d70627020f3b4c969cfd9bdc2474f4040c1ae976f687014694beb5d36dfc0c41bac8ebde96a14fceaee228d6e34d8183c5d7cc99d310d4f9"

meaning, you can generate some random string and put it into your config/initializers/secret_token.rb file:

# Be sure to restart your server when you modify this file.

Yourapp::Application.config.secret_token = '921b00fcfabe0368d70627020f3b4c969cfd9bdc2474f4040c1ae976f687014694beb5d36dfc0c41bac8ebde96a14fceaee228d6e34d8183c5d7cc99d310d4f9'
anarchivist
  • 1,387
  • 9
  • 20
Vlad Khomich
  • 5,820
  • 1
  • 27
  • 39
  • 8
    In Rails 3.2, ActiveSupport::SecureRandom no longer works. It is now just SecureRandom. It will look like this, "#{SecureRandom.hex(64)}" – MoB Aug 30 '12 at 18:04
4

This is an issue with rails version probably. I had this issue when I uninstalled Rails 4 and installed Rails 3. After checking rails -v and seeing that it was indeed Rails 3, I executed rails new myapp. For some reason the configuration file config/initializers/secret_token.rb had the "config.secret_key_base" variable defined, which appears to be how Rails 4 does it. I was able to fix it by changing it to "config.secret_token", which I believe is what Rails 3 uses.

Erich
  • 1,003
  • 1
  • 8
  • 9
0

This simple command worked for me :

rvmsudo rake generate_secret_token
benek
  • 2,088
  • 2
  • 26
  • 38
0

Make sure you have this in your environment.rb:

YourApp::Application.initialize!
Alextoul
  • 839
  • 3
  • 9
  • 19
0

Ran into this same issue and found out my config/initializers/secret_token.rb file was being ignored by git in my .gitignore file. Check out the config/initializers directory in your git source location and make sure the secret_token.rb file exists. If it doesn't edit your .gitignore file so that git will not ignore the secret_token.rb file and commit your changes (usually hidden - I used these simple commands to display hidden files on a mac http://osxdaily.com/2009/02/25/show-hidden-files-in-os-x/).

Mikael Kessler
  • 1,235
  • 1
  • 15
  • 27