使用设计时,如何将用户重定向到注册路由而不是登录路由

时间:2016-04-27 00:53:49

标签: ruby-on-rails ruby ruby-on-rails-4

我尝试覆盖authenticate_user方法但似乎导致无限循环

3 个答案:

答案 0 :(得分:0)

除非你知道自己在做什么,否则不应该破坏设计文件。你应该使用

if current_user something else link_to "Join up", new_user_registration_path

答案 1 :(得分:0)

    if user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end

答案 2 :(得分:0)

您可以轻松完成以下操作:

添加名为authenticate_user!的方法,如果用户未登录,则会覆盖默认方法以将用户重定向到注册路径。

应用程序控制器:

class ApplicationController < ActionController::Base

  before_action :authenticate_user!, :unless => :devise_controller? # This prevent the infinite redirects.

    protected
    def authenticate_user!
      if !current_user
          redirect_to new_user_registration_path    
      end
    end
end
相关问题