Skip to content

Commit 9ba3309

Browse files
leefaisonrbess
andauthored
Add sanitize search term method so that empty search entered does not return error page (#196)
* Add sanitize search term method so that empty search entered does not return error page Co-authored-by: Bess Sadler <[email protected]> * remove comments from test --------- Co-authored-by: Bess Sadler <[email protected]>
1 parent 0e30d6e commit 9ba3309

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

app/controllers/guide_cards_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def index
1111
end
1212

1313
def search
14-
@match = GuideCard.search_result(params[:search_term])
14+
@match = GuideCard.search_result(sanitize_search_term)
1515
@guide_cards =
1616
if params[:page].present?
1717
GuideCard.order(:id).page(params[:page])
@@ -20,6 +20,16 @@ def search
2020
end
2121
end
2222

23+
# This method sets an empty string to ***** in the search box so that
24+
# you do not hit an error page if no search term is entered.
25+
def sanitize_search_term
26+
if params[:search_term] == ''
27+
'*****'
28+
else
29+
params[:search_term]
30+
end
31+
end
32+
2333
def show
2434
@guide_card = GuideCard.find(params[:id])
2535
@sub_guide_cards = @guide_card.children

spec/system/search_feature_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,10 @@
3737
click_on 'Go'
3838
expect(page).to have_link('* Aaron')
3939
end
40+
it 'redirects user to first page of index if no search entered' do
41+
visit '/'
42+
click_on 'Go'
43+
expect(page).to have_link('*****')
44+
end
4045
end
4146
end

0 commit comments

Comments
 (0)