如何使设计注册控制器仅在用户已经登录时才显示sign_up页面? [英] How to make devise RegistrationsController to show sign_up page only if user is already signed in?

查看:86
本文介绍了如何使设计注册控制器仅在用户已经登录时才显示sign_up页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过google找到解决方案,但是找不到...

This 是唯一的问题。它只有一个答案,它被接受但不适用于我...这是我的代码:

I tried to find solution by google and here in SO but couldn't found...
This is the only question. It has only one answer and it's accepted but doesn't work for me... Here is my code:

class RegistrationsController < Devise::RegistrationsController

  before_filter :authenticate_user!

  def new
    puts "Method new was called"
    super
  end

end

当我未登录 localhost:3000 / sign_up 页面正常显示时, 打印方法new被调用。我想要控制器将我重定向到sign_in页面,如果我还没有登录。当然我可以检查它在新的方法和重定向,但它不是一个很好的解决方案..我相信有一个更优雅的方式。我甚至试图使用 prepend_before_filter:authenticate_user!,但它也不起作用。

When I'm not logged in at localhost:3000/sign_up page is displayed normally and Method new was called is printed. I want controller to redirect me into sign_in page if I'm not already signed in. Of course I can check it in new method and redirect but it's not a good solution... I'm sure there is a more elegant way. I even tried to use prepend_before_filter :authenticate_user! but it doesn't work too.

编辑

我在routs.rb中为这个控制器定义了路由

I've defined routes for this controller in routs.rb

devise_for :users, :controllers => { :sessions => "sessions", :registrations => "registrations" }


推荐答案

:: RegistrationsController 具有默认情况下,需要过滤之前的require_no_authentication。

Devise::RegistrationsController has require_no_authentication before filter by default.

所以需要跳过它:

class RegistrationsController < Devise::RegistrationsController
  skip_before_filter :require_no_authentication
  before_filter :authenticate_user!

  def new
    puts "Method new was called"
    super
  end

end

这篇关于如何使设计注册控制器仅在用户已经登录时才显示sign_up页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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