如何使色器件RegistrationsController显示sign_up页面只有当用户已经登录? [英] How to make devise RegistrationsController to show sign_up page only if user is already signed in?

查看:174
本文介绍了如何使色器件RegistrationsController显示sign_up页面只有当用户已经登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到由谷歌解决方案,并在这里SO但未能找到......结果
<一href=\"http://stackoverflow.com/questions/4213006/devise-before-filter-that-$p$pvents-access-to-new-user-registration-path-unless\">This是唯一的问题。它只有一个答案,它是公认的,但对我不起作用......这里是我的code:

 类RegistrationsController&LT;设计:: RegistrationsController  的before_filter:的authenticate_user!  高清新
    却将新方法被称为
    超
  结束结束

当我不在本地主机登录:3000 / sign_up 网​​页正常显示和新方法被称为被打印。我想控制器我重定向到sign_in页面,如果我尚未签署。当然,我可以检查它在方法和重定向,但它不是一个很好的解决方案..我敢肯定有一个更优雅的方式。我甚至试过用 prepend_before_filter:的authenticate_user ,但它不工作过。

修改

我已经为这个控制器routs.rb定义的路由

  devise_for:用户:控制器=&GT; {:会话=&GT; 会话:注册=&GT; 注册}


解决方案

设计:: RegistrationsController 有<一个href=\"https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L2\">require_no_authentication前过滤器在默认情况下的。

因此​​它需要跳过它:

 类RegistrationsController&LT;设计:: RegistrationsController
  skip_before_filter:require_no_authentication
  的before_filter:的authenticate_user!  高清新
    却将新方法被称为
    超
  结束结束

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

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.

EDIT

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

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

解决方案

Devise::RegistrationsController has require_no_authentication before filter by default.

So it's needed to skip it:

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

  def new
    puts "Method new was called"
    super
  end

end

这篇关于如何使色器件RegistrationsController显示sign_up页面只有当用户已经登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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