Skip to content

Commit

Permalink
Add sanitize search term method so that empty search entered does not…
Browse files Browse the repository at this point in the history
… return error page (#196)

* Add sanitize search term method so that empty search entered does not
return error page

Co-authored-by: Bess Sadler <bess@users.noreply.github.com>

* remove comments from test

---------

Co-authored-by: Bess Sadler <bess@users.noreply.github.com>
  • Loading branch information
leefaisonr and bess authored Oct 25, 2023
1 parent 0e30d6e commit 9ba3309
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/controllers/guide_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index
end

def search
@match = GuideCard.search_result(params[:search_term])
@match = GuideCard.search_result(sanitize_search_term)
@guide_cards =
if params[:page].present?
GuideCard.order(:id).page(params[:page])
Expand All @@ -20,6 +20,16 @@ def search
end
end

# This method sets an empty string to ***** in the search box so that
# you do not hit an error page if no search term is entered.
def sanitize_search_term
if params[:search_term] == ''
'*****'
else
params[:search_term]
end
end

def show
@guide_card = GuideCard.find(params[:id])
@sub_guide_cards = @guide_card.children
Expand Down
5 changes: 5 additions & 0 deletions spec/system/search_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@
click_on 'Go'
expect(page).to have_link('* Aaron')
end
it 'redirects user to first page of index if no search entered' do
visit '/'
click_on 'Go'
expect(page).to have_link('*****')
end
end
end

0 comments on commit 9ba3309

Please sign in to comment.