如何使用自定义控制器更改设计中的 redirect_to?

时间:2020-12-22 01:03:24

标签: ruby-on-rails ruby devise ruby-on-rails-6

我使用带有自定义控制器(用户控制器)的设计。与其登录并转到 user/edit,我更喜欢提供主页。为此,我需要编辑设计会话创建控制器方法。

我知道我可以按照设计提供的说明编辑设计自定义控制器:

  # before_action :configure_sign_in_params, only: [:create]

  # GET /resource/sign_in
  # def new
  #   super
  # end

  # POST /resource/sign_in
  # def create
  #   super
  # end

  # DELETE /resource/sign_out
  # def destroy
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
  # end

因此,按照这些说明,我如何简单地让 create 方法完全执行当前所做的操作,但是 redirect_to "/" 而不是它当前正在执行的操作?

注意 here 是它当前在做什么:

self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
yield resource if block_given?
respond_with(resource, serialize_options(resource))

另请注意,我可以直接从 github 上的 devise 复制该代码,然后对其进行编辑,如下所示:

self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
yield resource if block_given?
redirect to: "/"

但我想使用 super 方法执行此操作,因为这是设计说明中推荐的方法。

1 个答案:

答案 0 :(得分:2)

无需重新定义 create 操作,因为 Devise 允许您使用 after_sign_in_path_for 方法做到这一点。

def after_sign_in_path_for(resource)
  root_path
end
相关问题