更改注册控制器创建操作失败重定向路径

change registrationcontroller create action failure redirect path

我正在尝试在我的应用程序主页上实现注册表单。成功的流程(注册)完美无缺。问题在于验证失败时,即触发设备注册的重新呈现:新操作。我希望这种重新渲染发生在主页上而不是设计自己的视图。

我尝试通过将 root_path 位置添加到 "respond_with location" 来重写设计控制器,如下所示,但是由于某种原因,这会继续触发设计视图的渲染。

  # POST /resource
  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length

      respond_with resource, location: root_path # HERE!!!
    end
  end

我想知道解决这个问题的简洁方法是什么

编辑

让我震惊的是我可以使用首选模板渲染主页布局,但这感觉有点不合适。

  # POST /resource
  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length

      render layout: 'frontend/home', template: 'frontend/home/index'
    end
  end

我最终采用了上述编辑所附的方法:

  render layout: 'frontend/home', template: 'frontend/home/index'