Ruby-on-rails 使用 Rails 设计 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16513066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:57:58  来源:igfitidea点击:

Devise with Rails 4

ruby-on-railsdevisebundler

提问by BrainLikeADullPencil

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.

Devise 背后的团队通过博客文章http://blog.plataformatec.com.br/2013/05/devise-and-rails-4/宣布 它正在发布一个与 Rails 4 兼容的版本,称之为“3.0 rc” . 在同一篇博文中,它还说它正在发布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.

我正在尝试构建一个 Rails 4 应用程序。当我这样做时gem install Devise,它安装了 2.2.4,而不是与 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.

我从博客文章中关于强参数的评论中假设它与 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?

我查看了 Devise 的 github 页面,但我不清楚如何安装与 Rails 4 兼容的版本。你能帮忙吗?

https://github.com/plataformatec/devise

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

回答by sergserg

Devise is now compatible with Rails 4 out of the box as of the time of this answer.

截至本回答发布之时,Devise 现在与 Rails 4 兼容。

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.

安装设计 gem。

Open up your Gemfileand install the Devise gem.

打开Gemfile并安装 Devise gem。

gem 'devise'

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

然后在你的终端中运行bundle install命令来安装 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.

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

Generate your User model.

生成您的用户模型。

Next we need to generate our User model. I'm going to name it Userbut you can name it whatever you like, just replace Userwith Whatever.

接下来我们需要生成我们的用户模型。我要给它命名,User但你可以随意命名,只需替换UserWhatever.

rails generate devise User
rake db:migrate

Configure your default URL option for Development.rb

为 Development.rb 配置默认 URL 选项

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

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

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

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

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

You need to make sure that routes.rbhas a default root route - if you don't have one, set it!

你需要确保它routes.rb有一个默认的根路由——如果你没有,设置它!

root to: 'home#index'

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

创建一个局部视图以查看我们是否已登录。

Inside of your views/layoutsfolder create a file named _user_widget.html.erband copy this code in:

在您的views/layouts文件夹内创建一个名为的文件_user_widget.html.erb并将此代码复制到:

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

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

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

With all this in place, you should be able to sign up, log in and log out from your very own Rails website.

完成所有这些后,您应该能够注册、登录和退出您自己的 Rails 网站。

If you have any questions feel free to leave a comment below and I'll try to help.

如果您有任何问题,请随时在下面发表评论,我会尽力提供帮助。

回答by Zaid Crouch

UPDATE SEPT 17th, 2013: The master branch is now compatible with Rails 4. No need to search for another version.

2013 年 9 月 17 日更新:master 分支现在与 Rails 4 兼容。无需搜索其他版本。

Looking at the github repo, it looks like you want version 3.0.0.rc(no 1). So you'll want

查看github repo,看起来您想要版本3.0.0.rc(没有 1)。所以你会想要

gem install devise --version "3.0.0.rc"

or, in your gemfile:

或者,在您的 gemfile 中:

gem 'devise', '3.0.0.rc'

回答by BrainLikeADullPencil

This installed it

这安装了

gem install devise --pre

or

或者

devise-3.0.0.rc.gem

回答by Dhaulagiri

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

在这一点上,这个版本的 gem 就是你想要使用的

gem 'devise', '3.0.0'

回答by Kirsty Williams

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

gem 'devise',github:'plataformatec/devise',分支:'rails4'

回答by Spone

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

现在 3.0 版本稳定了,你可以这样做:

gem install devise

or in your Gemfile:

或在您的 Gemfile 中:

gem 'devise'