表单提交后设计覆盖重定向 [英] Devise override redirect after form submit

查看:24
本文介绍了表单提交后设计覆盖重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置我的 Rails 应用程序,以便在提交用于创建新用户的表单(通过设计)后,我重定向到我自己想要的页面?

How can I configure my Rails app such that after the form to create a new user is submitted (through devise), I redirect to my own desired page ?

谢谢

推荐答案

提交创建用户表单后,用户被创建并登录,因此您被重定向到的页面实际上是登录后的页面.如果您只想在创建用户时更改此页面,您可以在自定义注册控制器中设置 session["#{resource_name}_return_to"],如下所示:

After the create user form is submitted the user is created and then logged in so the page you are being redirected to is actually the after log in page. If you only want to change this page when a user is created you can set session["#{resource_name}_return_to"] in a custom registration controller like this:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    session["#{resource_name}_return_to"] = some_custom_path
    super
  end
end 

您还可以在 routes.rb 中为您的用户对象创建一个根路由,它会在所有用户登录时重定向:

You can also create a root route for your user object in routes.rb which will redirect all users whenever they log in:

match "user_root" => "users#home"

最后,您可以在 application_controller 中定义 after_sign_in_path_for(resource_or_scope) 方法,这将允许您有条件地重定向用户:

Finally you can define the after_sign_in_path_for(resource_or_scope) method in your application_controller and this will allow you to conditionally redirect users:

def after_sign_in_path_for(resource_or_scope)
  if resource_or_scope.is_a?(User)
    some_custom_path    
  else
    super
  end
end

这篇关于表单提交后设计覆盖重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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