为什么设计将 sign_up 错误重定向到不同的页面? [英] Why does Devise redirect sign_up error to a different page?

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

问题描述

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

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

我设计的模型是User,所以注册页面是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?

我阅读了这个问题唯一的答案建议在控制器中使用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.我应该如何覆盖 newcreate 来实现我需要的?

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 和:

First, go to your routes.rband:

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| %>

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

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