Rails 5 Devise:修改注册失败后的重定向路径
Hey there! Let's get that Devise redirect issue sorted out so your signup form errors land back on your landing page instead of the default /users/sign_up route. Here's a step-by-step fix:
Devise doesn't let you edit its default controllers directly, so we'll create a custom one to override the behavior:
rails generate devise:controllers users -c registrations
This creates app/controllers/users/registrations_controller.rb where we'll tweak the signup logic.
Open config/routes.rb and tell Devise to use your new controller instead of the default:
devise_for :users, controllers: { registrations: 'users/registrations' }
create Action to Handle Error Redirects Open the generated users/registrations_controller.rb and replace its content with this code. We're rewriting the signup logic to redirect to your root path (landing page) when validation fails, while preserving error messages:
class Users::RegistrationsController < Devise::RegistrationsController def create build_resource(sign_up_params) if resource.save # Keep Devise's default success behavior 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 # This is the key change: redirect to root instead of rendering /users/sign_up clean_up_passwords resource set_minimum_password_length redirect_to root_path, alert: resource.errors.full_messages.join(', ') end end end
Double-check that your landing page's signup form uses Devise's registration path. Here's a quick example of what the form should look like (adjust fields to match your setup):
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <!-- Display error messages if they exist --> <% if flash[:alert] %> <div class="error-box"> <%= flash[:alert] %> </div> <% end %> <div class="form-group"> <%= f.label :email %> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <div class="form-group"> <%= f.label :password %> <%= f.password_field :password, autocomplete: "new-password" %> </div> <div class="form-group"> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <%= f.submit "Sign Up", class: "btn-primary" %> <% end %>
To make sure error messages show up on your landing page, add this snippet to your application layout (e.g., app/views/layouts/application.html.erb):
<% if flash[:alert] %> <div class="alert alert-danger"> <%= flash[:alert] %> </div> <% end %>
That's it! Now when users submit the signup form with missing fields or invalid data, they'll be sent back to your landing page with clear error messages, instead of being redirected to the default Devise signup route.
内容的提问来源于stack exchange,提问作者Tikiboy

