I’ve just implemented some CSS tabs into my application (from here:
http://labs.silverorange.com/archives/2004/may/updatedsimple), but to
display
properly it requires that at least one item in the list is rather
than a
normal link, which I managed using this helper from leapingfrogs.com:
def link_to_unless_current_with_current_class(*args)
link_to_unless_current(*args) {|name| “#{name}”}
end
(which goes in module ApplicationHelper, if you fancy trying this
yourself). It
works perfectly but when you move onto a subitem the rails helper acts
properly
and makes your menu tab back into a link, spoiling the formatting.
What I’m looking for is an easy means of keeping the link to the
controller as
text, regardless of which action is being accessed. I think this is
possible
using something like link_to_unless (:controller = current
controller…) but
I’m not sure of the formatting. Can anyone please help?
Thanks in advance.
Ben wrote:
works perfectly but when you move onto a subitem the rails helper acts properly
and makes your menu tab back into a link, spoiling the formatting.
What I’m looking for is an easy means of keeping the link to the controller as
text, regardless of which action is being accessed. I think this is possible
using something like link_to_unless (:controller = current controller…) but
I’m not sure of the formatting. Can anyone please help?
Wow, thanks for the link to the tabs. I had some poor tabs before I saw
them.
Here’s how you can do this:
module ApplicationHelper
def link_to_unless_current_controller(controller_class, *args)
link_to_unless(controller.kind_of?(controller_class), *args) do
|name|
“#{name}”
end
end
end
In the view
<%
List of all the controllers here.
[ PostController,
AuthorController,
Admin::AuthorsController ].each do |c_class|
%>
<% c_human_name = c_class.name.demodulize[0..-11].underscore.humanize
%>
<%= link_to_unless_current_controller(c_class,
c_human_name,
{ :controller => '/' +
c_class.controller_path })
%>
<% end %>
Regards,
Blair
–
Blair Z., Ph.D.
[email protected]
Subversion and Orca training and consulting
http://www.orcaware.com/svn/
Blair Z. <blair@…> writes:
{ :controller => '/' +
c_class.controller_path }) %>
<% end %>
I’m incredibly grateful for your help, but I’m afraid I just
can’t get the code working properly. It run without an error,
but simply never produces a span instead of a link, so I guess
the condition isn’t firing.
Also, is there a way to test if something is a controller
without needing the full controller name? What’s the simplest method
of finding the current controller?
Thanks again.
What’s the error? You did change the list of controller classes, right?
Yes, and there is no error!
Regards,
Blair
The code works without an error, producing the tabs nicely, but it
simply
doesn’t produce the when on current controller - here’s an image
(with
message as the current
controller, it should be plain text rather then still a link)
http://img392.imageshack.us/img392/3634/tabs8mo.png
Thanks again.
Ben wrote:
<% c_human_name = c_class.name.demodulize[0…-11].underscore.humanize %>
but simply never produces a span instead of a link, so I guess
the condition isn’t firing.
What’s the error? You did change the list of controller classes, right?
Also, is there a way to test if something is a controller
without needing the full controller name? What’s the simplest method
of finding the current controller?
You can check if something is a controller via
object.instance_of?(ApplicationController). But you should not need
this, as
you should know the class of your variables.
The current controller is always in ‘controller’ in your views and
actions.
Regards,
Blair
Ben wrote:
of finding the current controller?
The code works without an error, producing the tabs nicely, but it simply
doesn’t produce the when on current controller - here’s an image (with
message as the current
controller, it should be plain text rather then still a link)
http://img392.imageshack.us/img392/3634/tabs8mo.png
Do you mean the current action or current controller?
In any case, here’s the code I used in the end. I removed the method I
added to
the helper, as the code was not needed here:
<%
[ PostController,
Admin::AuthorsController ].each do |c_class|
%>
-
<% c_human_name =
c_class.name.demodulize[0..-11].underscore.humanize %>
<% if controller.kind_of?(c_class) %>
<% action_name = controller.action_name.to_sym %>
<%= c_human_name %>
-
<% if [ :index, :list ].include?(action_name) %>
List
<% else %>
<%= link_to 'List', :action => :list %>
<% end %>
-
<% if :new == action_name %>
New
<% else %>
<%= link_to 'New', :action => :new %>
<% end %>
<% else %>
<%= link_to c_human_name,
{ :controller => '/' + c_class.controller_path } %>
<% end %>
<% end %>
Regards,
Blair