表单提交后设计覆盖重定向

Mar*_*ark 3 ruby-on-rails devise ruby-on-rails-3

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

谢谢

Bra*_*ker 7

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

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    session["#{resource_name}_return_to"] = some_custom_path
    super
  end
end 
Run Code Online (Sandbox Code Playgroud)

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

match "user_root" => "users#home"
Run Code Online (Sandbox Code Playgroud)

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

def after_sign_in_path_for(resource_or_scope)
  if resource_or_scope.is_a?(User)
    some_custom_path    
  else
    super
  end
end
Run Code Online (Sandbox Code Playgroud)