I have a navigation menu that contains a link per controller in my application. This menu is the same for all controllers, but I want this behaviour: <a>Users</a> <span>Statistics</span> and <span>Users</span> <a>Statistics</a> depending on the page you''re currently on. I''ve put a <render :partial => "_menu"> in the layout/application.rhtml file that is used as layout for all views. And in every view map, I then define a _menu.rhtml partial. This is of course not flexible enough, since when I want to change some name, I have to make changes in all these files. What is a better way? -- Posted via http://www.ruby-forum.com/.
Lieven De Keyzer wrote:> I have a navigation menu that contains a link per controller in my > application. > This menu is the same for all controllers, but I want this behaviour: > > <a>Users</a> > <span>Statistics</span> > > and > > <span>Users</span> > <a>Statistics</a> > > depending on the page you''re currently on. > > I''ve put a <render :partial => "_menu"> in the layout/application.rhtml > file that is used as layout for all views. And in every view map, I then > define a _menu.rhtml partial. > This is of course not flexible enough, since when I want to change some > name, I have to make changes in all these files. > What is a better way? >I don''t know how you are getting your controller names, but you can use a shared partial, and do something like: <% Dir[''app/controllers/*.rb'' ].each do |filename| controller_name = File.basename filename %> <% if @controller.controller_name == controller_name %> <span>%= controller_name %></span> <% else %> <a><%= controller_name %></a> <%end end %> @controller should be the controller that the request was made on, Zach
You may want to try something like this: <%= link_to_unless_current("Users", :action=>''users'') %> <%= link_to_unless_current("Statistics", :action=>''statistics'' ) %> On Thu, 15 Dec 2005, zdennis wrote:> Lieven De Keyzer wrote: >> I have a navigation menu that contains a link per controller in my >> application. >> This menu is the same for all controllers, but I want this behaviour: >> >> <a>Users</a> >> <span>Statistics</span> >> >> and >> >> <span>Users</span> >> <a>Statistics</a> >> >> depending on the page you''re currently on. >> >> I''ve put a <render :partial => "_menu"> in the layout/application.rhtml >> file that is used as layout for all views. And in every view map, I then >> define a _menu.rhtml partial. >> This is of course not flexible enough, since when I want to change some >> name, I have to make changes in all these files. >> What is a better way? >> > > I don''t know how you are getting your controller names, but you can use a > shared partial, and do something like: > > <% Dir[''app/controllers/*.rb'' ].each do |filename| > controller_name = File.basename filename %> > <% if @controller.controller_name == controller_name %> > <span>%= controller_name %></span> > <% else %> > <a><%= controller_name %></a> > <%end > end %> > > @controller should be the controller that the request was made on, > > Zach > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
zdennis wrote:> I don''t know how you are getting your controller names, but you can use > a shared partial, and do > something like: > > <% Dir[''app/controllers/*.rb'' ].each do |filename| > controller_name = File.basename filename %> > <% if @controller.controller_name == controller_name %> > <span>%= controller_name %></span> > <% else %> > <a><%= controller_name %></a> > <%end > end %> > > @controller should be the controller that the request was made on, > > ZachBut I might not want to create a link for each controller. -- Posted via http://www.ruby-forum.com/.
Ken Bowley wrote:> You may want to try something like this: > > <%= link_to_unless_current("Users", :action=>''users'') %> > <%= link_to_unless_current("Statistics", :action=>''statistics'' ) %>This won''t work for me, because: I want to have a menu on the page like: <%= link_to ''Test1'', :controller => ''test'', :action => ''index'' %> <%= link_to ''Test2'', :controller => ''test2'', :action => ''index'' %> Then, When I click on the first link, the menu will still be on the page, and above the content, there will be some other links, like this: <%= link_to ''Edit'', :controller => ''test'', :action => ''edit'' %> <%= link_to ''New'', :controller => ''test'', :action => ''new'' %> But for all these pages, I still want the menu to not display a link but a span for the current controller. I hope you understood my explanation? -- Posted via http://www.ruby-forum.com/.
On Wed, 21 Dec 2005, Lieven De Keyzer wrote:> Ken Bowley wrote: >> You may want to try something like this: >> >> <%= link_to_unless_current("Users", :action=>''users'') %> >> <%= link_to_unless_current("Statistics", :action=>''statistics'' ) %> > > This won''t work for me, because: > > I want to have a menu on the page like: > > <%= link_to ''Test1'', :controller => ''test'', :action => ''index'' %> > <%= link_to ''Test2'', :controller => ''test2'', :action => ''index'' %> > > Then, When I click on the first link, the menu will still be on the > page, and above the content, there will be some other links, like this: > > <%= link_to ''Edit'', :controller => ''test'', :action => ''edit'' %> > <%= link_to ''New'', :controller => ''test'', :action => ''new'' %> > > But for all these pages, I still want the menu to not display a link but > a span for the current controller. > > I hope you understood my explanation?Look up information on link_to_if Something similar to this might work for you: <%= link_to_if @controller!=''test'', ''Test1'', :controller => ''test'', :action => ''foo'' %>
Ken Bowley wrote:> Look up information on link_to_if > > Something similar to this might work for you: > > <%= link_to_if @controller!=''test'', ''Test1'', :controller => ''test'', > :action => ''foo'' %>Indeed, <ul> <% controller_name = @controller.controller_name %> <li><%= link_to_if controller_name != ''test'', ''Test'', :controller => ''test'', :action => ''index'' %> </li> </ul> did the trick. One more question, I used the temporary variable controller_name because I will need it in a lot of links. Should it be a regular variable or an instance variable? -- Posted via http://www.ruby-forum.com/.