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 条评论
登录 后参与评论

相关文章

来自专栏ASP.NETCore

ASP.NET Core 整合Autofac和Castle实现自动AOP拦截

除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninjec...

674
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2506
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.2K7
来自专栏菩提树下的杨过

Flash/Flex学习笔记(23):运动学原理

先写一个公用的小球类Ball: package{ import flash.display.Sprite; //小球 类 public class B...

25210
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

1990
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2667
来自专栏魂祭心

原 canvas绘制clock

4004
来自专栏落花落雨不落叶

canvas画简单电路图

58411
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5257
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3035

扫码关注云+社区