Rails 4设计 - 如果没有认证,重定向用户 [英] Rails 4 devise - redirect user if not authenticated

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

问题描述

在comments_controller中有 authenticate_user!

There is authenticate_user! in a comments_controller.

before_action :authenticate_user!

当用户点击创建评论时,他们现在被重定向到用户/ sign_in页面。

When a user clicks to create a comment, they are now redirected to users/sign_in page.

但是,我想将其重定向到root_url并发出通知登录。主页上有登录按钮(FB和G +)。

But, I want to redirect them to root_url and put a notice to log in. There are login buttons(FB and G+) on home page.

我看过 这个 ,但是当我实现它时,我只是得到一个黑色的页面,具有相同的URL和

I've looked at this, but when I implement it, I just get a black page with the same URL and Completed 401 Unauthorized.

推荐答案

您可以添加 authenticate_user! c $ c> application_controller.rb的方法

You can add authenticate_user! method to the application_controller.rb

class ApplicationController < ActionController::Base

  protected
  def authenticate_user!
    redirect_to root_path, notice: "You must login" unless user_signed_in?
  end
end

这篇关于Rails 4设计 - 如果没有认证,重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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