coletrain
coletrain

Reputation: 2849

Current controller path without specifying a action in rails

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?

Upvotes: 1

Views: 752

Answers (1)

Khaled
Khaled

Reputation: 2091

You could define a before_filter in your ApplicationController and name it set_menu

class ApplicationController < ActionController::Base
  before_filter :set_menu

  protected

  def set_menu
    @menu = 'default'
  end

end

Then in each controller that you want to show a different menu for you would override set_menu, for example:

class SkateMoviesController < ApplicationController

   protected

   def set_menu
     @menu = 'skate'
   end

end

you could use the helper method action_name in set_menu to access the current action in the controller.

Then in your views:

<% if @menu == 'default' %>
  <%= default_menu %>
<% else %>
  <%= skate_menu %>
<% end %>

Upvotes: 4

Related Questions