使用 Rails 设计 4 [英] Devise with Rails 4

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

问题描述

Devise 背后的团队通过博文宣布http://blog.plataformatec.com.br/2013/05/devise-and-rails-4/ 它发布了一个与 Rails 4 兼容的版本,称之为3.0 rc".在同一篇博文中,它还表示将发布 Devise 2.2.4.

我正在尝试构建一个 Rails 4 应用程序.当我执行 gem install Devise 时,它安装了 2.2.4,而不是与 Rails 4 兼容的版本.

获取:devise-2.2.4.gem (100%)

我从博客文章中关于强参数的评论中假设这与 Rails 4 不兼容.

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

https://github.com/plataformatec/devise

注意,我试过了

gem install devise --version 3.0.0.rc1

但它说

错误:在任何存储库中都找不到有效的 gem 'devise' (= 3.0.0.rc1)错误:可能的选择:设计

解决方案

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

我们的最终目标是让用户能够注册、登录和退出网站.我们还将创建一个小的局部视图,让我们知道我们是否已登录.

<小时>

安装 Devise gem.

打开你的 Gemfile 并安装 Devise gem.

gem '设计'

然后在您的终端中运行 bundle install 命令来安装 gem.

$ 捆绑安装

运行一些设计生成器来设置初始配置.

从终端运行此命令:

rails 生成设计:安装

这个生成器安装了用于配置所有 Devise 可用设置的初始化器.

生成您的用户模型.

接下来我们需要生成我们的用户模型.我将其命名为 User,但您可以随意命名,只需将 User 替换为 Whatever.

rails 生成设计用户耙数据库:迁移

为 Development.rb 配置默认 URL 选项

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

config.action_mailer.default_url_options = { :host =>'本地主机:3000' }

确保你在 Routes.rb 中声明了一个根路由

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

root 到:'home#index'

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

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

<% if user_signed_in?%><p>欢迎<%= current_user.email %></p><%= link_to '登录 [点击退出]', destroy_user_session_path, :method =>:删除%><%其他%><p>您尚未登录.</p><%= link_to '登录', new_user_session_path %><%结束%>

并在您的布局中调用它(views/layouts/application.html.erb):

<头><title>FacebookAuthTest</title><%= stylesheet_link_tag "application", media: "all" %><%= javascript_include_tag "应用程序" %><%= csrf_meta_tags %><身体><p class="notice"><%= notice %></p><p class="alert"><%=alert%></p><%=产率%><%= 渲染 'layouts/user_widget' %>

<小时><块引用>

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

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

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

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.


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.

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

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