Rails Devise Gem - ruby-on-rails

Am using rails 3.2.6 , ruby 1.8.7
I want add devise authentication in application..
I do following below mentioned in my app..
gem 'devise' add in Gemfile
after rails g devise:install in cmd prompt.
Add this line in developement.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Add this line in config/application.rb
config.assets.initialize_on_precompile = false
Finally, I run this cmd rails g devise user.
I got this error.
SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.
If you know answer for this error. please let me know..

You need to generate application token. It can be simply done by running:
rake secret
And add to config/initializers/secret_token.rb
AppName::Application.config.secret_token = 'CODE_HERE'
EDIT: Thanks #Nick

Related

Using config/secrets.yml in Rails 4.0.2 version

I was reading http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html
and saw there the trick with config/secrets.yml
I moved my secret_base_keys to that file, and removed secret_token.rb file.
But server doesn't start.
DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from service at /home/bismailov/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:138)
[2014-01-15 16:15:51] ERROR RuntimeError: You must set config.secret_key_base in your app's config.
I believe that is because I don't use Rails 4.1 yet.
Is there any way to implement this new functionality (secrets.yml) in Rails version 4.0? Maybe some kind of gem...
Thank you very much!
This secret_key_base deprecation does not seem to have alternative syntax to remove the deprecation warning in a Rails 4.0 application. To satisfy the deprecation, follow the steps for moving the production key to secrets.yml and delete the secret_token.rb file. The implement a YAML loader in your application.rb to extract the token from your secrets.yml file.
Use rake secret to generate a new token for each of your environments. Copy and paste the output to each section of your secrets.yml file.
# config/secrets.yml
development:
secret_key_base: __pasted from rake secret___
test:
secret_key_base: __pasted from rake secret___
production:
secret_key_base: __pasted token from config/initializers/secret_token.rb___
# config/application.rb
# TODO Remove this in Rails 4.1
config.secret_key_base = YAML.load(File.open("#{Rails.root}/config/secrets.yml"))[Rails.env]['secret_key_base']
Cite: https://github.com/rails/rails/pull/13298
UPDATE:
My original post focused on Inspired by #user2998870, I added a method to my application.rb that is allows one to implement multiple secrets, not just secret_key_base. This makes top-level keys accessible as methods e.g. Rails.application.secrets.braintree_merchant_id.
If nested, one can call the nested key value using Rails.application.secrets.braintree['merchant_key'].
Note: The original code above is still needed for secret_key_base to operate correctly in Rails 4.0.
# config/application.rb
def secrets
#secrets ||= begin
yaml = YAML.load(File.open("#{Rails.root}/config/secrets.yml"))[Rails.env]
ActiveSupport::OrderedOptions.new.merge!(yaml.symbolize_keys)
end
end
config/secrets.yml is a feature of Rails 4.1. Upgrade to Rails 4.1 to use the feature.
I did like #scarver2 mentioned, but I did it by borrowing some code from Rails 4.1 (I'm currently using on 4.0.3)
# Load 3rd party service passwords from config/services.rb.
# This is patch code to support config/services.rb till we upgrade to Rails 4.1.
# TODO: Remove this section after upgrading to Rails 4.1.
# Borrowed from rails/railties/lib/rails/application/configuration.rb
config.paths.add "config/secrets", with: "config/secrets.yml"
# Borrowed from rails/railties/lib/rails/application.rb
def secrets #:nodoc:
#secrets ||= begin
secrets = ActiveSupport::OrderedOptions.new
yaml = config.paths["config/secrets"].first
if File.exist?(yaml)
require "erb"
all_secrets = YAML.load(ERB.new(IO.read(yaml)).result) || {}
env_secrets = all_secrets[Rails.env]
secrets.merge!(env_secrets.symbolize_keys) if env_secrets
end
# Fallback to config.secret_key_base if secrets.secret_key_base isn't set
secrets.secret_key_base ||= config.secret_key_base
secrets
end
end

Why does my Rails 3.2.3 Application fail because my Engine plugin has devise installed

I have a Rails application called bdm_content_server
I have a Rails Engine plugin called bdmcs.
The engine is installed as a local file gem in my application.
gem "bdmcs", :path => "../bdmcs"
The Engine was running fine, and I was able to access it's controllers and views through application when running WEBrick.
I installed devise inside of the engine to control who had access to the engine's views. The setup went fine (with a little modification ala Rails engine and devise), and I'm able to execute the database migrations within the engine, but now when I attempt to migrate the Application that it's installed in I'm getting an error:
undefined method `secret_key=' for Devise:Module
/var/apps/bdm_content/bdmcs/config/initializers/devise.rb:7:in `block in <top (required)>'
/var/lib/gems/1.9.1/gems/devise-2.2.8/lib/devise.rb:267:in `setup'
/var/apps/bdm_content/bdmcs/config/initializers/devise.rb:3:in `<top (required)>'
The application doesn't appear to be acknowledging the engine's devise setup and configuration.
I have the devise gem installed in the Gemfile for the application, and if I remove it I get a different error:
cannot load such file -- devise
/var/apps/bdm_content/bdmcs/lib/bdmcs.rb:1:in `require'
/var/apps/bdm_content/bdmcs/lib/bdmcs.rb:1:in `<top (required)>'
Anyone have any thoughts?
I've searched around for similar questions but have only found ones related to either Devise in an Application or Devise in an Engine, but not one where both are involved.
Thanks in Advance
- Testero
Here's my /bdmcs/config/initializers/devise.rb file (as installed in my engine by Devise):
Devise.setup do |config|
config.secret_key = 'some_random_key'
config.mailer_sender = 'please-change-me-at-config-initializers-devise#example.com'
require 'devise/orm/active_record'
config.case_insensitive_keys = [ :email ]
config.strip_whitespace_keys = [ :email ]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 10
config.reconfirmable = true
config.password_length = 8..128
config.reset_password_within = 6.hours
config.sign_out_via = :delete
config.router_name = :bdmcs_user
end
And my /bdmcs/lib/bdcms.rb file:
require 'devise'
require "bdmcs/engine"
module Bdmcs
end
EDITED - Clarity and further detail
In your config/initializers/devise.rb file add this line:
config.secret_key = 'Your secret Key'
You can use rake secret to generate your secret key.
Make sure your using the latest version of Devise in your Gemfile.
Also make sure you restart your server. 99% of my problems went away after doing this.
I figured out that it was a Devise versioning issue.
For some reason, devise inside the engine was being installed at version 3.2.2 and the devise installed in the app was 2.2.8
Not sure how that happened, because I didn't specify any particular version, and both were generated in the same rails environment.
After I upgraded, I got a version conflict between Railties and Devise, so I ultimately had to upgrade Rails to 4.0.0 (and all the other things that come with that), and then it worked.
I am still having a bundler version problem, but I have resolved that using bundle exec ... to execute my rake and rails tasks.

ruby on rails authentication using devise gem

Am using devise gem for authentication
When i run rake db:migrate
I got the error mentioned below:
rake aborted!
User does not respond to 'devise' method.
This usually means you haven't loaded your ORM file or it's being loaded too late.
To fix it, be sure to require 'devise/orm/YOUR_ORM' inside 'config/initializers/devise.rb'
or before your application definition in 'config/application.rb'
If you know answer. Please let me know..
In the file config/initializers/devise.rb look for the line:
require 'devise/orm/active_record'
Make sure that it isn't commented out, and make sure it matches your orm.
If that file doesn't exist, then you haven't installed devise:
rails generate devise:install
Have a good read of the Getting Started instructions

rails 4.0, rake db:sessions:create

Rails 3.1 suggests running
rails generate session_migration
However this generates the exact same migration as
rake db:sessions:create
but none of the commands are recognized by my setup using rails 4.0
errors are :
Could not find generator session_migration.
and
Don't know how to build task 'db:sessions:create'
respectively.
I have run:
gem install 'activerecord-session_store'
How do I make it work so that i can store a shopping cart bigger than 4kb?
The ActiveRecord session store has been extracted out of Rails into it's own gem as part of Rails move towards better modularity. You need to include the gem as shown below in your Gemfile to get access to the rake task and related functionality.
gem 'activerecord-session_store', github: 'rails/activerecord-session_store'
The gem
The Rails commit where the change happened
A bit of an explanation
See the README of the gem linked above for more instructions, but you still need run the following command after installing the gem
rails generate active_record:session_migration
and after that you need to modify the config/initializers/session_store.rb to look like something like this
MyApp::Application.config.session_store :active_record_store, :key => '_Application_session'
or
Rails.application.config.session_store :active_record_store, :key => '_Application_session'
depending on your Rails version.

Ruby on Rails WEBrick SSL connection error

Please help to implement ssl
rails version - 3.2.8
I edtited following files:
# Gemfile
gem 'rack-ssl'
# config/application.rb
require 'rack/ssl'
config.middleware.use Rack::SSL
I also tried to use
# config/application.rb
config.force_ssl = true
But it shows
SSL connection error
when I access mysite:3000/
But it shows normal page if going to https:mysite
Please help,
thanks,
D
According to this:
How to use deactivate Webrick's SSL
The issue is caused by config.force_ssl = true. Even if you remove that, which you may not want, you might still have issues with WEBrick giving you this error. You could try clearing cookies, but that still might not work.
A better alternative, if it's an option for you, would be to switch to using the thin server:
group :development do
gem "thin"
end
Then:
$ bundle
$ thin start --ssl
See also https://stackoverflow.com/a/11614213

Resources