2014-07-13 33 views
2

我正在使用Rails 3.2.13,Devise 3.2.4。用户注册后,确认链接生成。设计 - 用户确认后如何重定向到其他页面

localhost:3000/users/confirmation?confirmation_token=HpPHitLtV3CnLy2z1ZmQ 

我想在点击确认链接后重定向到导师/新动作。

但确认后重定向到/用户/ sign_in我不想要的。以下是我迄今为止所做的代码。

路线

devise_for :users 
devise_for :users, :controllers => { :confirmations => 'confirmations' } 

创建确认书控制器和覆盖从设计:: ConfirmationsController

class ConfirmationsController < Devise::ConfirmationsController 
    protected 

def after_confirmation_path_for(resource_name, resource) 
    if resource.has_role? :user 
    redirect_to new_mentor_path("account_id") 
    else 
    root_path 
    end 
end 
end 

在ConfirmationsController,可能是我必须添加一些额外的代码。请提供一些解决方案。

在应用控制器我把下面的代码,这需要我的导师/学生家全成符号后在

def after_sign_in_path_for(resource_or_scope) 
if resource_or_scope.is_a?(User) 
    if current_user.user_type == "mentor" 
    home_mentors_path(:account_id => current_user.account_id) 
    else 
    home_students_path(:account_id => current_user.account_id) 
    end 
end 

def after_sign_up_path_for(resource_or_scope) 
    root_path 
end 

一般的逻辑是这样的,但我没有得到如何与Devise做到这一点。

def activate 
    @user = User.where("activation_code = ?","#{params[:id]}").first 
    if [email protected]? 
    if @user.created_at < 24.hours.ago 
    if @user and @user.activate 
     cookies[:user_id][email protected] 
     if @user.user_type == "student" 
     redirect_to new_student_path 
     else 
     redirect_to new_mentor_path 
     end 
    else 
     @user.destroy 
     redirect_to "/" 
    end 
    else 
    @user.destroy 
    redirect_to "/" 
    end 
else 
    redirect_to "/" 
end 

+1

看起来像'sign_in'重定向意味着你没有登录确认后?你能确认你的新用户在确认后登录吗? –

+0

您的问题有两个解决方案,有效相似,可以找到[这里] [1] [1]:http://stackoverflow.com/questions/10926626/devise-redirect-after-confirmation –

+0

@RichPeck :是的用户确认,我也登录。我只是想确认后,没有登录,它应该去辅导员/新页面。 –

回答

0

如果你看一下devise github repository。这是当你在确认链接现在

# GET /resource/confirmation?confirmation_token=abcdef 
def show 
    self.resource = resource_class.confirm_by_token(params[:confirmation_token]) 
    yield resource if block_given? 

    if resource.errors.empty? 
    set_flash_message(:notice, :confirmed) if is_flashing_format? 
    respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) } 
    else 
    respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new } 
    end 
end 

你重写色器件点击after_confirmation_path方法这样

protected 
def after_confirmation_path_for(resource_name, resource) 
    if resource.has_role? :user 
    redirect_to new_mentor_path("account_id") 
    else 
    root_path 
    end 
end 

这很好,并制定会重定向到您new_mentor_path但被调用的方法在你的导师控制器内部,你将会有一个之前的文件管理器来检查身份验证,例如:

before_action :authenticate_user! #this will check if your user is signed in or not and if not it'll redirect you 

因此,作为@Rich Peck建议你基本上需要也使您的用户在签署重写你的表演动作,他将被重定向到您的new_mentor_path

之前在你的表演动作,你可以做这样的事情,使确保您的用户在

# GET /resource/confirmation?confirmation_token=abcdef 
def show 
    self.resource = resource_class.confirm_by_token(params[:confirmation_token]) 

    if resource.errors.empty? 
    set_flash_message(:notice, :confirmed) if is_navigational_format? 
    sign_in(resource) 
    respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) } 
    else 
    respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new } 
    end 
end 

还签署了只注意到你的after_confirmation_path你有这样的redirect_to new_mentor_path("account_id")为什么你传递ACCOUNT_ID在你的路径字符串?

如果您ACCOUNT_ID是一个整数,那么你就必须首先把它show动作里,然后打电话给你的after_confirmation_path像respond_with_navigational(资源){redirect_to的after_confirmation_path_for(RESOURCE_NAME,资源,ACCOUNT_ID)}。你也必须改变after_confirmation_path喜欢的定义:

def after_confirmation_path_for(resource_name, resource, account_id) 
    if resource.has_role? :user 
    redirect_to new_mentor_path(account_id) 
    else 
    root_path 
    end 
end 
+0

资源:导师,:路径=>“/:account_id /导师”做,我已经在路线中使用。 account_id是来自导师/新人的许多事物的组合的字符串。我会试一试,谢谢。 –

+0

其实这个脚本帮助我。谢谢。 –

0

研究之后我张贴了这个问题的正确答案。

class ConfirmationsController < Devise::ConfirmationsController 
    def show 
    self.resource = resource_class.confirm_by_token(params[:confirmation_token]) 

if resource.errors.empty? 
    set_flash_message(:notice, :confirmed) if is_navigational_format? 
    sign_in(resource) #This will detect if user is logged in. 

    respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) } 
else 
    respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new } 
end 
end 

protected 
def after_confirmation_path_for(resource_name, resource) 

    if resource.is_a?(User) 
    new_mentor_path("account_id") #do not provide redirect_to which is already taken in respond_with_navigational(resource) 
    else 
    root_path 
    end 
end 
end 

我评论devise_for:用户,因为这条路被调用时,点击确认链接,确认控制器没有调用和重定向路径签署每次。所以下面是我面临的问题的主要解决方案。

# devise_for :users 
devise_for :users, :controllers => { :confirmations => 'confirmations' } 
+0

你的答案看起来和我的一样:) – Mandeep

+0

@Mandeep:答案不一样。我曾经使用devise_for:用户两次,这是问题。我经过评论后在路线上犯了错误,通过做一些修改解决了问题。 –

+0

@Mandeep:我试着从你的答案中获得解决方案,并且解决了问题,我赞成你。在我的回答中,我已经清楚地解释了我所面临的所有问题和解决方案,这一点很重要。所以你可以upvote我:) –

相关问题