【问题标题】:Redirect to a view after deleting account in Rails/Devise在 Rails/Devise 中删除帐户后重定向到视图
【发布时间】:2021-01-11 05:24:24
【问题描述】:

我是 Rails 新手,我有一个页面,用户可以使用 button_to 取消他们的帐户。但是,如何将用户重定向到另一个页面?例如,我想在删除他们的帐户时重定向到取消后页面说“再见”。

我尝试查看是否有 after_user_deletion 设计方法可以覆盖但没有。

这是我在 Devise 注册下的编辑视图中看到的内容。

    <div id="modal">
        <p>Are you sure you want to cancel?</p>
        <%= button_to "Yes", registration_path(resource_name), method: :delete%>
        <button>No</button>
    </div>

我不确定是否需要制作另一个控制器,但我制作了一个取消控制器。

class CancellationController < ApplicationController
    before_action :authenticate_user!

    def cancel
    
    end

end

这是我在 routes.rb 中的内容

Rails.application.routes.draw do
    devise_for :users
    root to: 'dashboard#index'
    devise_scope :user do
         get 'cancel' => 'cancellation#cancel'

    end

end

这是 rake 路由的内容:

RakeRoutes

【问题讨论】:

  • 在你的取消方法中,删除用户,然后写redirect_to post_cancellation_path,然后在路由中创建一个取消后路径并创建它的视图和控制器

标签: ruby-on-rails ruby model-view-controller devise


【解决方案1】:

您需要覆盖设计注册控制器上的重定向:

 # config/routes.rb

 devise_for :users, controllers: { registrations: 'registrations' }

这将指向您的控制器文件夹!所以你需要创建自己的设计控制器:

# app/controllers/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  # DELETE /resource
  def destroy
    # your own logic here, so you can redirect it after
  end

  def cancel
    expire_data_after_sign_in!
    redirect_to here_your_url
  end
end

注意:您在按钮上使用DELETE,触发销毁而不是取消,您可以检查设计控制器here

【讨论】:

    猜你喜欢
    • 2016-01-23
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多