为什么Devise将sign_up错误重定向到其他页面? [英] Why does Devise redirect sign_up error to a different page?

查看:161
本文介绍了为什么Devise将sign_up错误重定向到其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的rails项目,只需要一个主页生成的控制器来测试这个。

I created new rails project with just a generated controller for a home page to test this.

我设计的模型是用户,所以注册页面是 http:// localhost:3000 / users / sign_up

Me devise model is User, so the signup page is http://localhost:3000/users/sign_up.

如果我触发错误,例如,提交表单而不提供密码,我将被重定向到 http:// localhost:3000 / users

If I trigger an error, for example, submitting the form without giving password, I get redirected to http://localhost:3000/users.

发生错误后,如何保持 http:// localhost:3000 / users / sign_up

How can I stay on http://localhost:3000/users/sign_up after making errors?

我读了这个问题唯一的答案建议使用 redirect_to:back 在控制器中。

I read this question and the only answer suggests using redirect_to :back in the controller.

问题是我不会覆盖控制器。如果超越是解决的唯一方法,请说明应该如何。

The problem is I am not overriding the controller. If overriding is the only way to solve, please explain how it should be.

这是我的 routes.rb

Rails.application.routes.draw do
  root to: "home#index"
  get 'home/index'
  devise_for :users
end

这里 Devise的源代码:: RegistrationsController 。如何覆盖新的创建来实现我需要的东西?

Here is the source code of Devise::RegistrationsController. How should I override new or create to achieve what I need?

推荐答案

我强烈建议您不要这样做。 Rails应用程序的默认行为(遵循路由命名约定)就是这样描述的。但是,如果你真的需要这个,这是怎么做的。

I strongly recommend that you don't do this. The default behavior of Rails applications (following the route naming conventions) is that way you described. But, if you really need to to this, here's how.

首先,去你的 routes.rb

devise_scope :user do
    post 'users/sign_up', to: 'devise/registrations#create'
end

编辑

然后,生成设计视图,以便您可以覆盖表单的默认行为。

Then, generate the devise views so you can override the default behavior of the forms.

rails g devise:views

然后,打开您的 app / views / devise / registrations / new .html 文件,并编辑 form_for 行,看起来像这样:

Then, open your app/views/devise/registrations/new.html file and edit the form_for line so it looks like this:

<%= form_for(resource, as: resource_name, url: users_sign_up_path) do |f| %>

这篇关于为什么Devise将sign_up错误重定向到其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆