Rails里应用Devise

Use Devise For Authentication

Devise is great gem for authentication, check out here.

Add Gem

First thing you need to do is to add devise gem to you Gemfile. Just add gem 'devise', '~> 3.5' to your Gemfile and run bundle install and restart your server.

Set Up

Run generator rails generate devise:install to install an initializer. Devise will generate lots of file, git status to checkout. Also there will be some guides for what to do next in the terminal, just do as indicated! Something like make sure there is homepage, rails g devise:views and configuration stuff.

Generate User

Say you want to generate a user model. Just run rails g devise User and rake db:migrate. Then you can go to users/sign_up to create a new account.

Devise It

Authenticate Users

Now you can authenticate users! Say you want that only users that have signed in can edit or delete posts, otherwise, they can only go for index and show pages. It's pretty easy, just add this code to your posts_controller.rb file.

before_action: authenticate_user!, except: [:index, :show]

Then unauthenticated users will be redirected to sign up page if they want to edit or delete posts.

Show Edit and Delete Link for Users Signed In

If you want the Edit and Delete button shown for users signed in. Add this code to your view.

<% if user_signed_in? %>
    <div id="admin_links">
        <%= link_to "Edit Article", edit_post_path(@post) %>
        <%= link_to "Delete Article", post_path(@post), method: :delete, data: {confirm: "Are you sure?"} %>
    </div>
<% end %>
Show Sign Out Link for Users Singed In

You may also want add a sign out link for users who have signed in. To do this, add this code to your _header.html.erb partial.

<% if user_signed_in? %>
    <%= link_to "Sign Out", destroy_user_session_path, method: :delete %>
<% end %>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏程序员的酒和故事

Go实战--golang中使用RethinkDB(gorethink/gorethink.v3)

生命不止,继续go go go !!! 关于golang中操作数据库,曾经介绍了不少: Go实战–go语言操作sqlite数据库(The way to go) ...

4168
来自专栏向治洪

Android 应用安装过程分析

在之前的文章中,我们对PakageManagerService启动流程分析 做了简单的介绍,并对PMS系统的启动流程做了详细的解析。上面只是说到了Android...

3639
来自专栏生信宝典

Cytoscape教程(一)

What is cytoscape Cytoscape is an open source software platform for visualizing...

2906
来自专栏pangguoming

IIS 安装 pydio

Introduction In this how-to, we’ll see the installation of Pydio (Put Your Data ...

38011
来自专栏陈满iOS

[iOS源码笔记]·第三方网络下载处理框架:AFNetworking网络下载处理(调用栈与缓存策略篇)

看到这里,或许可以看manager对象的AFHTTPSessionManager类型更深层定义。

762
来自专栏用户2442861的专栏

GDB调试Segmentation Fault

http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html

502
来自专栏数据和云

Oracle Hints - 先知的提示

在上周恩墨微信大讲堂的讨论中,几个有趣的视图跃入我们的视野,可以分享给大家。 在Oracle 11g中,新增的视图V$SQL_HINT记录了Oracle数据库中...

2716
来自专栏Hadoop实操

如何使用HBase存储图片

2232
来自专栏算法+

3D Lut 电影级调色算法 附完整C代码

长话短说,3d lut(全称 : 3D Lookup table )它是通过建立一个颜色映射表,对图像的色调进行重调的算法。

69310
来自专栏挖掘大数据

常用的Hadoop 文件查看工具

packages.config <?xml version="1.0" encoding="utf-8"?> <packages> <package id...

1839

扫码关注云+社区