繁体   English   中英

用户更新设计后​​的几个重定向

[英]Several redirects after user update with devise

我正在尝试创建一个rails 5应用程序,其设计应该像社交网络一样,因此每个用户都有一个配置文件。 在更新用户而不是返回到配置文件之后,服务器重定向到localhost,然后有一条消息Filter chain halted as :require_no_authentication rendered or redirected ,然后才加载用户配置文件。 同样在配置文件中会显示一条消息“您已登录”。 我的目标是删除那些重定向。

  1. 我试图在自定义RegistrationController更改重定向,但后来它说有太多的重定向,所以我不能替换它。
  2. 我尝试在routes.rb为经过身份验证和未经身份验证的用户设置不同的根,但它没有更改行为
  3. 我试图落后于:require no authentication但我实际上并不确定我是否正确理解它并且它没有改变任何东西。
  4. after_sign_in_path_for设置为用户配置文件

这是服务器输出:

Started PUT "/users" for 127.0.0.1 at 2019-06-04 12:13:37 +0200
Processing by Users::RegistrationsController#update as HTML
  Parameters: {"email"=>"asdf@asdf.de", "password"=>"[FILTERED]", "password_confirmat
ion"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
User Load (0.8ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT
 1
   (0.3ms)  BEGIN
   (0.4ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 295ms (ActiveRecord: 2.1ms)


Started GET "/" for 127.0.0.1 at 2019-06-04 12:13:37 +0200
Processing by Users::SessionsController#new as HTML
  User Load (0.7ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER
 BY `users`.`id` ASC LIMIT 1
Redirected to http://localhost:3000/users/1
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 15ms (ActiveRecord: 0.7ms)


Started GET "/users/1" for 127.0.0.1 at 2019-06-04 12:13:37 +0200
Processing by UsersController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER
 BY `users`.`id` ASC LIMIT 1
  User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT
 1
  Rendering users/show.html.erb within layouts/application
  Rendered users/show.html.erb within layouts/application (1.1ms)
  Rendered shared/_navbar.html.erb (0.7ms)
Completed 200 OK in 162ms (Views: 145.3ms | ActiveRecord: 1.1ms)

另外,为什么用户在更新后会再次登录? 这是正确的行为吗? 我似乎无法理解重定向到localhost的地方正在发生和介入。 任何帮助,将不胜感激。

编辑1

所以,澄清一下。 我尝试使用authenticated_root但后来我得到错误消息Couldn't find User without an ID

devise_scope :user do

authenticated :user do
    root to: 'users#show', as: :authenticated_root
  end
  root to: 'users/sessions#new'
end

此外,我无法将重定向从更改为root更改为用户配置文件。 所以RegistrationController没有变化。 我想在更新方法或其他地方“更新后转到用户配置文件”中说。

您的root_path似乎是/users/sign_in ,当用户更新其帐户时,您将其重定向到根...但用户已登录,因此无法访问new_user_session_pah 在Devise中配置的后备默认位置是用户的配置文件,因此他被重定向到此路由。

您是否配置了经过身份验证的根? https://github.com/plataformatec/devise/wiki/How-To:-Define-a-different-root-route-for-logged-in-out-users

它应该解决你的问题(:

我设法通过向RegistrationController添加一个方法来解决它,如下所示:

def after_update_path_for(resource)
  user_path(current_user)
end

也就是说,在更新用户之后,Rails转到root_path,即sessions#new 但是用户已经登录,因此rails路由器会将您重定向到users#show

为什么不设置root "home#index" (或者像那样)

Rails.application.routes.draw do
  devise_for :users

  authenticated :user do
    root 'secret#index', as: :authenticated_root
  end

  root "home#index"
end

然后,在您的application_controller.rb您可以要求用户在使用您的页面之前登录

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!
end

如果要将用户重定向到users#show after_update_path_for after update,则可以覆盖after_update_path_for

class User::RegistrationsController < Devise::RegistrationsController

  protected

  def after_update_path_for(resource)
    user_path(resource)
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM