Devise with Rails 4

2019-01-16 10:45发布

问题:

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

回答1:

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.



回答2:

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'


回答3:

This installed it

gem install devise --pre

or

devise-3.0.0.rc.gem


回答4:

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

gem 'devise', '3.0.0'


回答5:

gem 'devise', github: 'plataformatec/devise', branch: 'rails4'



回答6:

Now that the 3.0 version is stable, you can just do:

gem install devise

or in your Gemfile:

gem 'devise'