Skip to content

Commit

Permalink
Merge pull request consuldemocracy#4837 from joaoGabriel55/fix_bug_wh…
Browse files Browse the repository at this point in the history
…en_creating_admin_tags

Fix bug when creating admin tags
  • Loading branch information
Senen committed Jun 8, 2022
2 parents 2b45e2f + e767350 commit 494f1de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/controllers/admin/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
class Admin::TagsController < Admin::BaseController
before_action :find_tag, only: [:update, :destroy]
before_action :tags, only: [:index, :create]

respond_to :html, :js

def index
@tags = Tag.category.page(params[:page])
@tag = Tag.category.new
@tag = Tag.category.new
end

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

def destroy
Expand All @@ -20,6 +26,9 @@ def destroy
end

private
def tags
@tags ||= Tag.category.page(params[:page])
end

def tag_params
params.require(:tag).permit(:name)
Expand Down
11 changes: 11 additions & 0 deletions spec/system/admin/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,15 @@

expect(page).to have_content "Soon a category"
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

0 comments on commit 494f1de

Please sign in to comment.