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 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.