Arif
Arif

Reputation: 742

current_page? isn't working with routes with optional params

Here's the route:

get "search(/:search)", to: "posts#index", as: :search

Now if I'm at /search/somethingsomething and the view is:

- if current_page?(search_path)
  = (do something)

then that something isn't being done. If I remove the parenthesis around :search, however (i.e. get "search/:search" ...), then it works. What gives?

Upvotes: 1

Views: 628

Answers (2)

Arif
Arif

Reputation: 742

Never mind. Solved it by replacing the condition with:

- if current_page?("/search/#{params[:search]}")

Upvotes: 0

Michael Malov
Michael Malov

Reputation: 1887

You can use this condition

current_page?(controller: 'posts', action: 'index')

when you use search_path in condition it generated without optional parameters

Upvotes: 1

Related Questions