Devise redirect back on sign up failure with validations

I have a simple Devise registration form with the validatable plugin. It mostly works as intended, if the user forgets to enter a first name it redirects them back with a red validation message.

The problem is it redirects to the same path a successful login would have the user go to (i.e. it redirects them to /user and not back to /user/sign_up). If the user then refreshes the page for whatever reason they get a No route matches [GET] "/user" error.

How can I force a redirect to go back to the original /user/sign_up route on a sign up failure? I know I can hack the create action in the registration controller but when I redirect to the proper route I lose the Devise validation messages.


It appears the problem is the way Devise handles respond_with Rails 4 How Overwrite Devise Respond Path Upon Error. It looks like Devise tries to render a create template at /user but I still can't override it.


First you can create devise controllers using following command -

rails generate devise:controllers users

Then you have to modify your routes for devise

# config/routes.rb
Rails.application.routes.draw do

  devise_for :users,
         :skip => [:registrations]

  devise_scope :user do
    get "user/sign_up", to: "users/registrations#new", as: :new_user_registration
    post "user/sign_up", to: "users/registrations#create", as: :user_registration


Hope it's work.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

  • Devise: redirect on sign up failure?
    I'm trying to redirect users that have failed the sign up form (e.g. they entered a username that is already taken, they left a field blank, etc...) I have custom failure set up for users that fail the sign in form, code below: class CustomFailure < Devise::FailureApp def redirect_url root_path end def respond if http_auth? http_auth else redirect end end However, I've been stuck on how to set this up for sign up failure. Ideally I would just like to redirect them back/to root_path, any ideas? Thank you!
  • 将用户重定向到注册(Redirect User to Signup)
    问题 使用before_action :authenticate_user! 检查用户是否登录。但它会发送用户login而不是signup 。 尝试了不同的方式引导用户signup而不是login ,但他们在注册成功后不会将用户返回到原始页面。 如何发送用户注册并在之后将用户引导回原始页面? 尝试: before_filter :auth_user def auth_user redirect_to new_user_registration_url unless user_signed_in? end 路由文件 Rails.application.routes.draw do devise_for :installs resources :orders resources :products devise_for :users get 'dashboard' => 'pages#dashboard' get 'contact' => 'pages#contact' get 'cart' => 'carts#index' root 'pages#home' 回答1 您正在寻找的是推荐人。 让你的 before_filter 工作,你可以覆盖设计的after_sign_up_path_for : def after_sign_up_path_for(user) request
  • Devise omniauthable breaks Omniauth authentication with `Could not find a valid mapping for path`
    In my project, I have two type of users: job seekers and hiring managers. Job seekers don't have a model, they are just able to apply for jobs using the data received from from third-party providers while authenticating thru Omniauth. Hiring managers' info is stored in devise User model. Hiring managers also must be able to sign in with their company's Google email account. So, first I built job seekers' authentication using Omniauth 1.0.0, Rails 3.1.3: omniauth.rb require 'omniauth-openid' require 'openid/store/filesystem' Rails.application.config.middleware.use OmniAuth::Builder do provider
  • 在登录或注册后设计重定向回原始位置?(Devise redirect back to the original location after sign in or sign up?)
    问题 在这里,我使用 Devise Gem 进行身份验证。 如果有人想在没有登录的情况下打开页面,那么它会重定向到 sign_in 页面,然后在登录后返回到用户尝试打开的页面。 我使用重定向循环和设计 after_sign_in_path_for 链接来解决我的问题,但它对我不起作用。 def after_sign_in_path_for(resource) params[:next] || super end 它不会将我重定向回我要打开的页面。 例如:如果我想打开“”,登录后不返回此页面。 回答1 寻求的最佳资源是官方 repo/wiki/issues,然后是 SO。 您找到的答案已过时。 这是答案:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up, -更新 只需在 ApplicationController 中为版本 devise > 3.2.1添加以下内容: # This example assumes that you have setup devise to authenticate a class named User. class
  • Rails, Devise & Omniauth - problems with setup
    I am trying (again) to set up authentications with Rails 4, devise and omniauth. I tried to follow the example in this post: Rails 4, Devise, Omniauth (with multiple providers) I have these gems installed: gem 'devise' gem 'omniauth-google-oauth2' gem 'omniauth-facebook' gem 'omniauth-twitter' gem 'omniauth-linkedin-oauth2' gem 'oauth2' I have a user model, and an authentications model. I have: User.rb: has_many :authentications def disapprove self.approved = false end def approve self.approved = true end SOCIALS = { facebook: 'Facebook', google_oauth2: 'Google', linkedin: 'Linkedin' } def
  • Rails、Devise 和 Omniauth - 设置问题(Rails, Devise & Omniauth - problems with setup)
    问题 我正在尝试(再次)使用 Rails 4、devise 和 omniauth 设置身份验证。 我试着按照这篇文章中的例子:Rails 4, Devise, Omniauth (with multiple providers) 我安装了这些宝石: gem 'devise' gem 'omniauth-google-oauth2' gem 'omniauth-facebook' gem 'omniauth-twitter' gem 'omniauth-linkedin-oauth2' gem 'oauth2' 我有一个用户模型和一个身份验证模型。 我有: 用户.rb: has_many :authentications def disapprove self.approved = false end def approve self.approved = true end 社交 = { 脸书:'脸书',google_oauth2:'谷歌',linkedin:'Linkedin'} def self.from_omniauth(auth, current_user) authentication = Authentication.where(:provider => auth.provider, :uid => auth.uid.to_s, :token => auth
  • Rails 4 如何覆盖设计响应路径出错(Rails 4 How Overwrite Devise Respond Path Upon Error)
    问题 我一直在为这个而苦苦挣扎。 我有一个 Rails4/Devise 3.1 应用程序,系统中有两个用户: 毕业生雇主 和一个设计用户,可以通过多态:profile关联成为毕业生或雇主。 我让毕业生通过/graduate/sign_up路径注册,雇主通过/employer/sign_up路径注册,这两者都指向相同的/views/devise/registrations/new.html.erb视图(因为他们的注册表单几乎相同 -电子邮件和密码)。 一切正常,除非出现验证错误, RegistrationsController#create.respond_with resource总是将两种用户类型重定向到/users路径,我需要将它们重定向回它们最初来自的位置,例如/graduate/sign_up或/employer/sign_up分别。 我试过用redirect_to替换respond_with ,但我最终丢失了带有相关错误消息的资源对象。 关于如何做到这一点的任何想法? 这些是我的模型: class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable belongs_to
  • 使用Omniauth为无STI的多个模型进行设计(Devise with Omniauth for multiple models without STI)
    问题 有什么方法可以为没有STI的多个模型配置Devise Omniauth? 我们有学生模型和教授模型,我们不想使用STI,但是现在我们意识到带Omniauth的Devise在多个模型中不能很好地发挥作用。 .rvm/gems/ruby-1.9.3-p125/gems/devise-2.1.0/lib/devise/rails/routes.rb:384:in `devise_omniauth_callback': Wrong OmniAuth configuration. If you are getting this exception, it means that either: (RuntimeError) 1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one 2) You are setting :omniauthable in more than one model 3) You changed your Devise routes/OmniAuth setting and haven't restarted your server 回答1 我认为,与其写作, :omniauthable是个别Student和Professor model
  • AngularJS:对多个模型使用devise(AngularJS: Use devise with multiple models)
    问题 我有一个使用devise的Rails应用程序,它具有2种不同的模型,分别对应于非常不同的角色(没有STI-完全不同的模型)。 我打算迁移到AngularJS的前端。 想知道什么是构建应用程序的最佳方法。 这是我最初的想法: 每个用户模型都有一个不同的登录页面(domain.com/user和domain.com/company) 在呈现该页面之前,我使用devise检查是否已登录正确的角色,以及是否未重定向至相应的登录页面(服务器端重定向) 所有未登录的页面都是非角度的,这样才能有效地实现。 登录页面均为Angular限定 这确保了我不必担心使角度和设计协同工作良好。 在一个模块中,我可以确定已登录的用户(或公司) 请建议听起来是否不错。 此外,理想情况下,我希望整个应用程序可以(不是太复杂)使用Angular。 (登录重定向到相应的登录页面等)。 我已经看到了一些以服务模块等形式出现的Angular + Devise的示例,但是几乎所有这些示例似乎都只能用于一种模型。 编辑:理想情况下,我希望能够使用Devise的尽可能多的功能。 回答1 我建议您重新考虑一下自己的问题,假装设计与角度无关。 假设您不会在Rails中呈现任何html模板。 您将使用Rails创建一个json api http://railscasts.com/episodes/350-rest-api
  • 过滤器链因 :validate_sign_up_params 渲染或重定向而停止 - devise_token_auth(Filter chain halted as :validate_sign_up_params rendered or redirected - devise_token_auth)
    问题 我正在使用devise_token_auth作为身份验证处理 ruby​​ on rails API,每次我从postman google chrom扩展程序进行 API 调用时都会收到此错误。以下是我的设置 文件 gem 'devise' gem 'omniauth' gem 'devise_token_auth' gem 'byebug' gem 'rack-cors', :require => 'rack/cors' 路由文件 mount_devise_token_auth_for 'User', at: 'auth' 应用控制器.rb include DeviseTokenAuth::Concerns::SetUserByToken before_action :authenticate_user! , :except=>[:new, :create] 用户模型: devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable include DeviseTokenAuth::Concerns::User after_initialize :set_provider #
  • 覆盖设计 SessionsController#Create 并创建视图 - 并且身份验证失败(Overriding Devise SessionsController#Create and create view - and authentication fails)
    问题 我有一个用户登录页面,它改编自设计登录视图,我覆盖了控制器,因为我需要在登录时执行多项任务,但我无法通过第 1 步(管理员身份验证。 在 Users::SessionController < Devise::SessionsController 里面,我只是有 def create super [...other post authentication code] end 调试到 super.create 我可以看到它调用 self.resource = warden.authenticate!(auth_options) 但它永远不会从调用 super 中返回。 不知何故,监狱长。验证! 调用似乎劫持了 create 方法最终重新呈现登录页面。 我看到 auth_options 参数只包含一个散列: => {:scope=>:user, :recall=>"users/sessions#new"} 也许那里出了什么问题,因为我认为电子邮件和密码需要以某种方式传递,但无论如何,Warden 有点神秘,不确定这些身份验证选项中应该包含什么。 所以我真的不明白什么是被认证的,但无论如何,监狱长对我来说都是一个谜。 这是传递给 create 方法的 params 集合。 <ActionController::Parameters {"utf8"=>"✓",
  • Rails 4 - 设计,来宾用户导致过滤器链停止(Rails 4 - Devise, guest users causes filter chain halted)
    问题 我刚刚开始开发一个 Rails 4 (4.2.3) 应用程序,我使用Devise进行用户身份验证。 我希望用户能够在注册之前通过创建测试项目并以访客用户身份登录来试用该应用程序。 当用户注册(或注册)时,我想将测试项目分配给新的当前用户。 我一直在关注 Platformatec 的本指南:https://github.com/plataformatec/devise/wiki/How-To:-Create-a-guest-user 创建来宾用户有效,但在注册或使用活动来宾用户会话时,我收到以下错误: Filter chain halted as :require_no_authentication rendered or redirected 如果我清除我的会话它就可以工作。 管理我的访客用户的方法如下所示: def current_or_guest_user if current_user if session[:guest_user_id] && session[:guest_user_id] != current_user.id logging_in guest_user(with_retry = false).try(:destroy) session[:guest_user_id] = nil end current_user else guest_user end
  • Rails 4 How Overwrite Devise Respond Path Upon Error
    I've been struggling for a while with this one. I have a Rails4/Devise 3.1 app with two users in the system: Graduates Employers and one devise User who can be either a Graduate or Employer via a polymorphic :profile association. I have Graduates sign up via /graduate/sign_up path and employers via /employer/sign_up path both of which route to the same /views/devise/registrations/new.html.erb view (since their signup form is pretty much the same - email and password). Everything works fine, except when there is a validation error, RegistrationsController#create.respond_with resource always
  • Filter chain halted as :validate_sign_up_params rendered or redirected - devise_token_auth
    I'm working on a ruby on rails API using devise_token_auth as authentication , and i'm getting this error everytime i'm making a API call from postman google chrom extension .below are my settings Gemfile gem 'devise' gem 'omniauth' gem 'devise_token_auth' gem 'byebug' gem 'rack-cors', :require => 'rack/cors' routes.rb mount_devise_token_auth_for 'User', at: 'auth' application_controller.rb include DeviseTokenAuth::Concerns::SetUserByToken before_action :authenticate_user! , :except=>[:new, :create] User model: devise :database_authenticatable, :registerable, :recoverable, :rememberable,
  • Devise redirect back to the original location after sign in or sign up?
    Here I'm using Devise Gem for authentication. If someone want to open page without login then it redirect to sign_in page and after signed in it back to the page which user try to open. I use Redirect loop with Devise after_sign_in_path_for link for my problem but it does not work for me. def after_sign_in_path_for(resource) params[:next] || super end It doesn't redirect back me to the page which I want to open. for example: If I want to open "", it doest not back to this page after signed in.
  • 允许管理员使用 Devise 添加用户(Allowing admins to add users with Devise)
    问题 我正在努力做到这一点,只有管理员才能使用设计添加用途。 我已经得到它的大部分工作,但是现在当我以管理员身份登录并提交注册表时,它会以错误提示我: You are already signed in. 我试图按照这里的说明进行操作:http://wiki.summercode.com/rails_authentication_with_devise_and_cancan 但它似乎没有提到这种情况。 我是否需要在editors_controller进一步覆盖以允许这样做? 这是我的路线(“编辑器”是我的用户模型的名称): devise_for :admins, :skip => [:registrations] as :admin do get 'admin/editors' => 'editors#index', as: :admin_editors get 'admin/editors/new' => 'editors#new', as: :new_editor delete 'admin/editors/:id' => 'editors#destroy', as: :destroy_editor end devise_for :editors, :skip => [:registrations], :controllers => { :registrations =>
  • How to add authentication before filter to a rails 3 apps with devise for user sign up and sign in?
    Given I'm on Rails 3.1, Ruby 1.9.2 with a standard setup for devise to log a user in by name and password (e.g. similar to https://github.com/RailsApps/rails3-devise-rspec-cucumber ): I need to add a layer that authenticates a posted username and password against an external service before creating a new user (sign up) or when a user signs in. (FWIW, further down the road I plan on using the https://github.com/chicks/devise_aes_encryptable strategy/gem to encrypt the sensitive password and, when logging in with a local password, decrypt the one for the remote service, authenticate, then
  • 更改密码后用户会话无效,但仅限于多线程(User sessions invalid after changing password, but only with multiple threads)
    问题 我在 Rails 4 + Devise 3.2 应用程序中遇到了一个奇怪的问题,它允许用户通过 AJAX POST 将他们的密码更改为以下操作,该操作源自 Devise wiki 允许用户编辑他们的密码。 好像是在用户修改了密码之后,经过一个或多个请求后,他们会被强制注销,并且在重新登录后会继续被强制注销。 # POST /update_my_password def update_my_password @user = User.find(current_user.id) authorize! :update, @user ## CanCan check here as well if @user.valid_password?(params[:old_password]) @user.password = params[:new_password] @user.password_confirmation = params[:new_password_conf] if @user.save sign_in @user, :bypass => true head :no_content return end else render :json => { "error_code" => "Incorrect password" }, :status => 401 return
  • 设计/Omniauth 失败:如何调试?(Devise/Omniauth failure: How to debug it?)
    问题 尝试“使用 Google 登录”后,我在日志中看到此错误: Processing by Users::OmniauthCallbacksController#failure as HTML 我可以看到通过 URL(在日志中)发送的来自 google 的所有数据,包括用户电子邮件和姓名。 那么会出现什么问题呢? 我的回调甚至没有被执行。 我只会被重定向到我网站的登录页面。 而且我很确定一切都配置正确,因为几周前它运行良好。 我不认为我改变了什么。 Facebook 登录仍然可以正常工作。 关于如何调试此失败的任何想法? 除了那些充满参数和值的长 URL 之外,日志中没有其他内容。 只有 INFO 消息。 上面张贴的是唯一一个说失败的人。 更新 我向控制器添加了一个“失败”方法 def failure render :text => params.inspect end 这停止了​​重定向,并打印了这个: {} 网址是这样的: /users/auth/google/callback?_method=post&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=id_res&openid.op_endpoint=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8
  • Overriding Devise's registration controller to allow for a redirect after a successful sign_up has been done
    I have looked all over the place, and found a lot of info... but nothing works for me and I don't get it :( I know that you are suppose to override the registration controller, like this: class Users::RegistrationsController < Devise::RegistrationsController def after_sign_up_path_for(resource) authors_waiting_path end end Then following the example showed by Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/, I am supposed to change my routes to update the access the new controller: devise_for :users, :controllers => {