ruby-on-rails 如何在按下提交按钮后重定向到特定页面(RAILS 7)

8yoxcaq7  于 2023-07-01  发布在  Ruby
关注(0)|答案(2)|浏览(134)

在填写我的注册表格并点击提交后,它会将我重定向到我的根页面,我不想这样做,我希望它重定向到我创建的特定页面。我正在使用rails 7,我使用de devise gem。
my registration_controller.rb

class  RegistrationController < ApplicationController 
    before_action :configure_permitted_parameters, if: :devise_controller?

    protected
  
    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:prénom, :nom, :age, :gender, :tel_number])
      devise_parameter_sanitizer.permit(:update, keys: [:prénom, :nom])
    end
    end

我的路线.rb

Rails.application.routes.draw do
  resources :information
  devise_for :accounts

  root "pages#home"


  devise_scope :account do
    get 'accounts/sign_out' => "devise/sessions#destroy"
end

  get 'accounts/verify_email', to: 'pages#VerifyEmail'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end

我尝试在我的registration_controller.rb中执行此操作,但没有成功。

def new
      @account = Account.new
    end
  
    def create
      @account = Account.new(params[:accounts])
      if @account.save
        redirect_to accounts_verify_email
      else
        render :new
      end
ahy6op9u

ahy6op9u1#

首先,将注册控制器基于Devise::RegistrationsController
然后你就有了这个可以覆盖的方法。

class RegistrationsController < Devise::RegistrationsController
  protected

  def after_sign_up_path_for(resource)
    '/an/example/path' # Or :prefix_to_your_route
  end
end

您也可以使用任何路径帮助器方法。resource是可用的,以防万一,如果你有更多的可验证的模型。但我想你不需要那个了。

mbzjlibv

mbzjlibv2#

首先,您需要告诉路由器使用自定义设备注册控制器

# config/routes.rb

devise_for :accounts, { registrations: 'registrations' }

然后创建它并覆盖after_sign_up_path_for Devise方法

# app/controllers/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  private

  def after_sign_up_path_for(_resource)
    some_specific_path
  end
end

相关问题