Redirect to edit path on success

Simply rendering the edit form on the posted path is problematic. For example if you refresh the path you get a 404.
But if there's errors, we want to render the form with unsaved values so you can see the errors and try again.
This commit is contained in:
David Cook
2023-09-28 09:27:09 +10:00
parent c1587b689a
commit e844d71abc
2 changed files with 13 additions and 4 deletions

View File

@@ -33,8 +33,8 @@ module Spree
@user.spree_roles = roles.compact_blank.collect{ |r| Spree::Role.find(r) }
end
flash.now[:success] = Spree.t(:created_successfully)
render :edit
flash[:success] = Spree.t(:created_successfully)
redirect_to edit_admin_user_path(@user)
else
render :new
end
@@ -50,9 +50,11 @@ module Spree
@user.spree_roles = roles.compact_blank.collect{ |r| Spree::Role.find(r) }
end
flash.now[:success] = update_message
flash[:success] = update_message
redirect_to edit_admin_user_path(@user)
else
render :edit
end
render :edit
end
protected

View File

@@ -22,6 +22,13 @@ describe Spree::Admin::UsersController do
it "allows admins to update a user's show api key view" do
user.spree_roles << Spree::Role.find_or_create_by(name: 'admin')
spree_put :update, id: test_user.id, user: { show_api_key_view: true }
expect(response).to redirect_to spree.edit_admin_user_path(test_user)
end
it "re-renders the edit form if error" do
user.spree_roles << Spree::Role.find_or_create_by(name: 'admin')
spree_put :update, id: test_user.id, user: { password: "blah", password_confirmation: "" }
expect(response).to render_template :edit
end