From f942768ce829ec2f66cf106b4a54dc85c4a591a8 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 13 Nov 2022 01:58:58 -0600 Subject: [PATCH] Fix #5347: Don't use exception template for post validation errors Also fixes #5173: Parenting a post to itself using the "parent id" box leads to an error. --- app/controllers/posts_controller.rb | 9 +++------ app/views/posts/partials/show/_edit.html.erb | 4 +--- app/views/posts/show.html.erb | 4 +--- test/functional/posts_controller_test.rb | 9 ++++++++- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f6dcc669c..a80db103a 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -174,13 +174,10 @@ class PostsController < ApplicationController end if post.errors.any? - @error_message = post.errors.full_messages.join("; ") - render :template => "static/error", :status => 500 - else - response_params = {:q => params[:tags_query], :pool_id => params[:pool_id], :favgroup_id => params[:favgroup_id]} - response_params.reject! {|_key, value| value.blank?} - redirect_to post_path(post, response_params) + flash[:notice] = post.errors.full_messages.join("; ") end + + redirect_to post_path(post, { q: params[:q] }.compact_blank) end format.json do diff --git a/app/views/posts/partials/show/_edit.html.erb b/app/views/posts/partials/show/_edit.html.erb index 9caf57728..5db032311 100644 --- a/app/views/posts/partials/show/_edit.html.erb +++ b/app/views/posts/partials/show/_edit.html.erb @@ -7,9 +7,7 @@ <%= render_source_data(nil) %> <%= edit_form_for(post, html: { id: "form" }) do |f| %> - <%= f.input :tags_query, as: :hidden, input_html: { id: nil, name: "tags_query", value: params[:q] } %> - <%= f.input :pool_id, as: :hidden, input_html: { id: nil, name: "pool_id", value: params[:pool_id] } %> - <%= f.input :favgroup_id, as: :hidden, input_html: { id: nil, name: "favgroup_id", value: params[:favgroup_id] } %> + <%= f.input :q, as: :hidden, input_html: { id: nil, name: "q", value: params[:q] } %> <%= f.input :old_tag_string, as: :hidden, input_html: { value: post.tag_string } %> <%= f.input :old_parent_id, as: :hidden, input_html: { value: post.parent_id } %> <%= f.input :old_source, as: :hidden, input_html: { value: post.source } %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 20aa680e1..3a4d52c41 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -299,9 +299,7 @@
<%= edit_form_for(@post, url: mark_as_translated_post_path(@post), method: :put) do |f| %> - <%= f.input :tags_query, as: :hidden, input_html: { id: nil, name: "tags_query", value: params[:q] } %> - <%= f.input :pool_id, as: :hidden, input_html: { id: nil, name: "pool_id", value: params[:pool_id] } %> - <%= f.input :favgroup_id, as: :hidden, input_html: { id: nil, name: "favgroup_id", value: params[:favgroup_id] } %> + <%= f.input :q, as: :hidden, input_html: { id: nil, name: "q", value: params[:q] } %>
<%= f.input :check_translation, as: :boolean, input_html: { checked: @post.has_tag?("check_translation") } %> diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 35083f2fe..689d1e65f 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -787,7 +787,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest end context "update action" do - should "work" do + should "redirect to the post on success" do put_auth post_path(@post), @user, params: {:post => {:tag_string => "bbb"}} assert_redirected_to post_path(@post) @@ -812,6 +812,13 @@ class PostsControllerTest < ActionDispatch::IntegrationTest assert_response 403 assert_not_equal("blah", @post.reload.tag_string) end + + should "not raise an exception on validation error" do + put_auth post_path(@post), @user, params: { post: { parent_id: @post.id }} + assert_redirected_to post_path(@post) + + assert_nil(@post.parent_id) + end end context "destroy action" do