diff --git a/app/controllers/kudos_controller.rb b/app/controllers/kudos_controller.rb index cd2b946fe9c..27d483e77b3 100644 --- a/app/controllers/kudos_controller.rb +++ b/app/controllers/kudos_controller.rb @@ -41,8 +41,7 @@ def create respond_to do |format| format.html do flash[:kudos_notice] = t(".success") - redirect_path = url_from(request.referer) || polymorphic_path(@kudo.commentable) - redirect_to "#{redirect_path}#kudos_message" and return + redirect_to kudos_redirect_path(@kudo.commentable) and return end format.js do @@ -59,8 +58,7 @@ def create return if check_user_status flash[:kudos_error] = error_message - redirect_path = url_from(request.referer) || polymorphic_path(@kudo.commentable || root_path) - redirect_to "#{redirect_path}#kudos_message" and return + redirect_to kudos_redirect_path(@kudo.commentable || root_path) and return end format.js do @@ -78,8 +76,7 @@ def create respond_to do |format| format.html do flash[:kudos_error] = error_message - redirect_path = url_from(request.referer) || polymorphic_path(@kudo&.commentable || root_path) - redirect_to "#{redirect_path}#kudos_message" and return + redirect_to kudos_redirect_path(@kudo&.commentable || root_path) and return end format.js do @@ -90,6 +87,11 @@ def create private + def kudos_redirect_path(commentable) + redirect_path = url_from(request.referer) || polymorphic_path(commentable) + "#{redirect_path.split('#', 2).first}#kudos_message" + end + def kudo_params params.require(:kudo).permit(:commentable_id, :commentable_type) end diff --git a/spec/controllers/kudos_controller_spec.rb b/spec/controllers/kudos_controller_spec.rb index 082a1be6b9f..650d853df25 100644 --- a/spec/controllers/kudos_controller_spec.rb +++ b/spec/controllers/kudos_controller_spec.rb @@ -19,6 +19,12 @@ it_redirects_to_with_kudos_notice(referer, "Thank you for leaving kudos!") end + it "redirects without a duplicate anchor when the referer already has an anchor" do + request.headers["HTTP_REFERER"] = work_path(work, anchor: "bookmark-form") + post :create, params: { kudo: { commentable_id: work.id, commentable_type: "Work" } } + it_redirects_to_with_kudos_notice(work_path(work, anchor: "kudos_message"), "Thank you for leaving kudos!") + end + it "does not save user on kudos" do post :create, params: { kudo: { commentable_id: work.id, commentable_type: "Work" } } expect(assigns(:kudo)).to be_persisted