Hello,
I’m still dtarting with ruby on rails. And I’m trying to have a menu
depending on the current controller/method.
Here is what I was thinking of (please tell me if there’s a better way
to do it):
I have a database with menu_groups (corresponding to controllers) and
menu_elements(corresponding to methods). Each menu_element belongs to
one menu_group.
I created a class Menu where I get all the menu_groups and
menu_elements, and where I have the active controller and method.
"
class Menu
attr_reader :menu_groups
attr_reader :menu_group_active
attr_reader :menu_elements
attr_reader :menu_element_active
def initialize
current_url_elements=request_uri().split("/")
@menu_groups << MenuGroup.find(:name,:order => "id")
@menu_group_active = controller.controller_name
@menu_elements << MenuElement.find(:all,:order => "menu_group_id
id")
@menu_elements_active = current_url_elements[-1]
end
end
"
Then, thanks to the attributes of this class I’ll be able in my layout
to make the difference between active and non active groups/elements.
The problem here is that I don’t know how to get the url into a
variable…
If my method is good, can you please tell me how to make it works?
If not, can you show me the right way to do this??
Thanks a lot
Best regards,
Matthieu
Unless I am missing something…Make layouts out of your menus then do
something like this
In your controllers or even your application.rb
layout :select_layout
then this method at the end of your controller
private
def select_layout
[‘action_name1’, ‘action_name2’].include?(action_name) ? “layout1”
: “layout2”
end
Storing references to a menu in a DB seems wrong.
Help that helps.
I add something:
I have one common layout for all my controllers.
Kim wrote:
Make layouts out of your menus
Do you mean, I have to do one layout for every situation my menu will be
in (=the number of method I’ll have)??
It seems a lot to me and maybe I’m wrong but it would induce a lot of
redundency, no?
In your controllers or even your application.rb
layout :select_layout
then this method at the end of your controller
private
def select_layout
[‘action_name1’, ‘action_name2’].include?(action_name) ? “layout1”
: “layout2”
end
what does ‘action_name’ represent ?
No - you make a layout for the menu - how many menus do you have? Your
menu should retain a consistant look across most of your
controllers/actions - unless I am not understanding something. I don’t
understand what you mean by “the situation my menu will be in (=the
number of method I’ll have)” If you mean, for example, that you want
the “comments” menu item to be a different color when you are
performing that action then use CSS and helper methods like
link_to_unless_current amongst others.
It seems like you might want to read some ofthe multiple of books this
list preaches about.
def select_layout
['action_name1', 'action_name2'].include?(action_name) ? "layout1"
: “layout2”
endwhat does ‘action_name’ represent ?
action_name is the name of the action that should use layout1.