简体   繁体   中英

Rails 4 - current_page? with query parameter

I've method in my application.rb

def current_page(path)
  "active" if current_page?(path)
end

then in my view, lets say the request_uri is http://www.example.com/stories , i just can call

<%= link_to "All Stories", stories_path, :class => "#{current_page(stories_path)}" %>

BUT how about if the request_uri has something like http://www.example.com/stories?featured=true ? How do I call it in view?

Because current_page? method will compare passed-in path which doesn't have ? just with your current url deleting query params.

For example, if you're at http://www.example.com/stories?featured=true , and you have current_page(stories_path) , you'll still compare http://www.example.com/stories (stories_path) with http://www.example.com/stories (your current url without featured=true ).

I have a idea, but you'll have ? in the end of your normal url.You can change the code to <%= link_to "All Stories", stories_path + '?', :class => "#{current_page(stories_path + "?")}" %>

I'll update this if I come up with better methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM