The team behind Devise announced via blogpost
http://blog.plataformatec.com.br/2013/05/devise-and-rails-4/ that it was releasing a version that is compatible with Rails 4, calling it '3.0 rc'. In the same blog post, it also said it's releasing Devise 2.2.4.
I'm trying to build a Rails 4 app. when I did gem install Devise
, it installed 2.2.4, not the version compatible with Rails 4.
Fetching: devise-2.2.4.gem (100%)
Which I assume from the comments in the blogpost about strong parameters is not going to be compatible with Rails 4.
I looked at Devise's github page but it's not obvious to me how to install the version compatible with Rails 4. Can you assist?
https://github.com/plataformatec/devise
Note, I tried
gem install devise --version 3.0.0.rc1
but it said
ERROR: Could not find a valid gem 'devise' (= 3.0.0.rc1) in any repository
ERROR: Possible alternatives: devise
Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).
The devise gem is basically based on a warden gem, which gives an opportunity to build authorization direct on a Ruby Rack Stack. This gem is pretty straightforward and well documented. Warden fetches a request data and checks if the request includes valid credentials, according to a defined strategy.
devise_for is meant to play nicely with other routes methods. For example, by calling devise_for inside a namespace, it automatically nests your devise controllers: namespace :publisher do devise_for :account end. The snippet above will use publisher/sessions controller instead of devise/sessions controller.
Devise is now compatible with Rails 4 out of the box as of the time of this answer.
Our end goal is to have users be able to register, log in and log out of the website. We'll also create a small partial view letting us know if we're logged in or out.
Open up your Gemfile
and install the Devise gem.
gem 'devise'
Then in your terminal run the bundle install
command to install the gem.
$ bundle install
Run this command from your terminal:
rails generate devise:install
This generator installs the initializer that configures all of Devise's available settings.
Next we need to generate our User model. I'm going to name it User
but you can name it whatever you like, just replace User
with Whatever
.
rails generate devise User rake db:migrate
Inside of config/environments/development.rb
, set the Action Mailer's default URL to localhost:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
You need to make sure that routes.rb
has a default root route - if you don't have one, set it!
root to: 'home#index'
Inside of your views/layouts
folder create a file named _user_widget.html.erb
and copy this code in:
<% if user_signed_in? %> <p>Welcome <%= current_user.email %></p> <%= link_to 'Logged In [click to logout]', destroy_user_session_path, :method => :delete %> <% else %> <p>You are not signed in.</p> <%= link_to 'Login', new_user_session_path %> <% end %>
And invoke it within your layout (views/layouts/application.html.erb
):
<!DOCTYPE html> <html> <head> <title>FacebookAuthTest</title> <%= stylesheet_link_tag "application", media: "all" %> <%= javascript_include_tag "application" %> <%= csrf_meta_tags %> </head> <body> <p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p> <%= yield %> <%= render 'layouts/user_widget' %> </body> </html>
Make sure you stop and restart the server otherwise you will find all sorts of nasty bugs! It's always best to restart your local server when you update your gemfile or change anything in the environment configuration file.
With all this in place, you should be able to sign up, log in and log out from your very own Rails website.
If you have any questions feel free to leave a comment below and I'll try to help.
UPDATE SEPT 17th, 2013: The master branch is now compatible with Rails 4. No need to search for another version.
Looking at the github repo, it looks like you want version 3.0.0.rc
(no 1). So you'll want
gem install devise --version "3.0.0.rc"
or, in your gemfile:
gem 'devise', '3.0.0.rc'
This installed it
gem install devise --pre
or
devise-3.0.0.rc.gem
At this point this version of the gem is what you would want to use
gem 'devise', '3.0.0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With