I have a rails application that has 2 menu in which the menus changes depending on what page the users are currently visiting. Is there any way to tell rails that if a user is visiting this controller, no matter if the visitor is on the index,edit,create,update,delete method?
I am currently using a helper like so it and it indeed a bit messy.
def which_page
current_page?(root_path) ||
current_page?(skate_movies_path) ||
current_page?(new_skate_photos_path(@user)) ||
current_page?(skate_photos_path) ||
current_page?(skate_tricks_path)
end
In My view partial
<% if which_page %>
<%= default_menu %> #upload, #photos, #trick-tips, #goals
<% else %>
<%= skate_menu %> #dashboard, #shared-videos, #profile
<% end %>
The problem is this works but throughout the application I always find a page or two where it gives me a routing error. Any way to tell what controller the user is on and action without specifying ever action?
You could define a
before_filterin yourApplicationControllerand name itset_menuThen in each controller that you want to show a different menu for you would override
set_menu, for example:you could use the helper method
action_nameinset_menuto access the current action in the controller.Then in your views: