Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise with Rails 4

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
like image 704
BrainLikeADullPencil Avatar asked May 12 '13 23:05

BrainLikeADullPencil


People also ask

What does devise do in Rails?

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

What is devise warden?

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.

What is Devise_for?

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.


4 Answers

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.


Install the Devise gem.

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 some Devise generators to set up the initial configurations.

Run this command from your terminal:

rails generate devise:install 

This generator installs the initializer that configures all of Devise's available settings.

Generate your User model.

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 

Configure your default URL option for Development.rb

Inside of config/environments/development.rb, set the Action Mailer's default URL to localhost:

config.action_mailer.default_url_options = { :host => 'localhost:3000' } 

Make sure you have a root route declared in Routes.rb

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' 

Create a partial view to see if we're logged in or not.

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.

like image 66
sergserg Avatar answered Oct 08 '22 23:10

sergserg


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' 
like image 44
zkcro Avatar answered Oct 08 '22 23:10

zkcro


This installed it

gem install devise --pre

or

devise-3.0.0.rc.gem
like image 39
BrainLikeADullPencil Avatar answered Oct 09 '22 00:10

BrainLikeADullPencil


At this point this version of the gem is what you would want to use

gem 'devise', '3.0.0'
like image 34
Dhaulagiri Avatar answered Oct 08 '22 23:10

Dhaulagiri