gpt4 book ai didi

ruby-on-rails - Rails&Devise : Failed registration attempt redirects to root url

转载 作者:行者123 更新时间:2023-12-03 12:19:02 25 4
gpt4 key购买 nike

当用户尝试注册失败时,我的应用会将他们重定向到根URL,而不是呈现注册页面。

路线:

resources :users
devise_for :users, path: '', path_names: { sign_up: 'register', sign_in: 'login', sign_out: 'logout'}, :controllers => { :omniauth_callbacks => "callbacks" }

注册#新:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:class => "col s12 form-text", autocomplete: "off"}) do |f| %>

<%# render 'devise/errors', resource: resource %>
<div class="row">
<div class="input-field col s12">
<%= f.text_field :full_name, :class => "validate", :required => true, :placeholder => "Robin Smith" %>

<%# f.label :full_name, :class => "allcaps active", :data => {:error => 'Name Required'} %>
<%= f.label :full_name, :class => "allcaps" %>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<%= f.email_field :email, :class => "validate", :required => true, placeholder: " " %>
<%= f.label :email, :class => "allcaps" %>
</div>
</div>

<div class="row">
<div class="input-field col s12">
<%= f.password_field :password, :class => "validate", :required => true, placeholder: " " %>
<%= f.label :password, "password (min 6 chars)", :class => "allcaps" %>
</div>
</div>

<div class="button-container">
<button class="waves-effect btn-flat btn-large"type="submit" name="action">Join Now</button>
</div>
<% end %>

我可以转到mydomain.com/register并显示表格。但是,如果注册尝试失败,它们将被重定向到根url,而不是用错误重新呈现注册页面。

我觉得我的路线一定有什么问题。任何想法?

最佳答案

您可以覆盖Devise创建操作并重定向到您的网址:

# registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController

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
resource.errors.full_messages.each {|x| flash[x] = x}
#redirect_to your_path
end
end

end

并在 route :
resources :users
devise_for :users, path: '', path_names: { sign_up: 'register', sign_in: 'login', sign_out: 'logout'}, :controllers => { :registrations => "registrations", :omniauth_callbacks => "callbacks" }

关于ruby-on-rails - Rails&Devise : Failed registration attempt redirects to root url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42012902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com