Merge pull request #4837 from joaoGabriel55/fix_bug_when_creating_admin_tags

Fix bug when creating admin tags
This commit is contained in:
Senén Rodero
2022-06-08 13:57:01 +02:00
committed by GitHub
2 changed files with 24 additions and 4 deletions

View File

@@ -1,17 +1,23 @@
class Admin::TagsController < Admin::BaseController class Admin::TagsController < Admin::BaseController
before_action :find_tag, only: [:update, :destroy] before_action :find_tag, only: [:update, :destroy]
before_action :tags, only: [:index, :create]
respond_to :html, :js respond_to :html, :js
def index def index
@tags = Tag.category.page(params[:page]) @tag = Tag.category.new
@tag = Tag.category.new
end end
def create def create
Tag.find_or_create_by!(name: tag_params["name"]).update!(kind: "category") @tag = Tag.find_or_initialize_by(tag_params)
redirect_to admin_tags_path save_and_update = @tag.save && @tag.update!(kind: "category")
if save_and_update
redirect_to admin_tags_path
else
render :index
end
end end
def destroy def destroy
@@ -20,6 +26,9 @@ class Admin::TagsController < Admin::BaseController
end end
private private
def tags
@tags ||= Tag.category.page(params[:page])
end
def tag_params def tag_params
params.require(:tag).permit(:name) params.require(:tag).permit(:name)

View File

@@ -105,4 +105,15 @@ describe "Admin tags", :admin do
expect(page).to have_content "Soon a category" expect(page).to have_content "Soon a category"
end end
scenario "Create shows validation error when tag name is empty" do
visit admin_tags_path
within("form.new_tag") do
fill_in "tag_name", with: ""
click_button "Create topic"
end
expect(page).to have_content "can't be blank"
end
end end