设计与轨道4 [英] Devise with Rails 4

查看:149
本文介绍了设计与轨道4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Devise后面的团队通过blogpost
http://blog.plataformatec.com.br/2013/05/devise-and-rails-4/ ,它正在发布与Rails 4兼容的版本,称为3.0 rc。在同一篇博文中,它也表示正在发布 Devise 2.2.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.

我正在尝试建立一个Rails 4应用程序当我做了 gem安装Devise ,它安装了2.2.4,而不是与Rails 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%) 

我从博客中关于强参数的评论中假设不会与Rails 4兼容。

Which I assume from the comments in the blogpost about strong parameters is not going to be compatible with Rails 4.

我看着Devise的github页面,但对我来说并不明显安装与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

注意,我尝试

gem install devise --version 3.0.0.rc1

但它说

ERROR:  Could not find a valid gem 'devise' (= 3.0.0.rc1) in any repository
ERROR:  Possible alternatives: devise


推荐答案

Devise现在与Rails 4开箱即用兼容这个答案。

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.

打开您的 Gemfile 并安装Devise宝石。

Open up your Gemfile and install the Devise gem.

gem 'devise'

然后在你的终端运行 bundle install 命令来安装gem。

Then in your terminal run the bundle install command to install the gem.

$ bundle install



运行一些Devise生成器来设置初始配置。



从您的终端运行此命令:

Run some Devise generators to set up the initial configurations.

Run this command from your terminal:

rails generate devise:install

此生成器安装初始化程序,配置所有Devise的可用设置。

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



为Development.rb配置您的默认网址选项



config / environments / development.rb 内部,将Action Mailer的默认URL设置为localhost:

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' }



确保您在Routes.rb中声明了根路由



您需要确保 routes.rb 具有默认的根路由 - 如果没有,则将其设置!

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'



创建部分视图,看看我们是否登录。



在您的视图/布局文件夹创建一个名为 _user_widget.html.erb 的文件,并复制以下代码:

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

并在您的布局( 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>








确保您停止并重新启动服务器,否则您会发现各种恶作剧!当您更新gemfile或更改
环境配置中的任何内容时,最好重新启动本地
服务器文件。

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.

有了这一切,您应该可以注册,登录并从您自己的Rails网站注销

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.

这篇关于设计与轨道4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆