更改registrationcontroller创建操作失败重定向路径

时间:2018-03-28 23:37:15

标签: ruby-on-rails devise

我正在尝试在我的应用的主页上实现注册表单。成功的流程(注册)非常有效。问题在于验证失败,即触发设计注册的重新注册:新操作。我希望这个重新发布在主页上发生,而不是设计自己的观点。

我试图通过将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

1 个答案:

答案 0 :(得分:0)

I eventually went with the approach as appended with the edit above:

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