Forum: Ruby on Rails Conditionally assigning html id to link_to (tabbed interface)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Vinay (Guest)
on 2008-10-11 06:43
(Received via mailing list)
Hi all,
I am looking to implement a tabbed interface. The tabs are mere
link_to links in the template. All i need to do is, check if a
particular tab has been selected and if it has been selected, set
the :id attribute to 'current'. The css code will take care of the
rest. I know the css works because when i explicitly set the :id =>
'current without any conditions, it shows in the browser.
I have however tried many variations and all are throwing me syntax
errors. Hope someone can give me the right syntax for this or at least
show a better way of implementing this.

<%= link_to 'Companies', companies_path(), :id=>"current" unless
controller.controller_name != 'Companies' %>
<%= link_to 'Companies', companies_path(), if
controller.controller_name == 'Companies' :id=>"current" end%>
<%= link_to 'Companies', companies_path(), if
controller.controller_name != 'Companies' :id=>"current" %>
<%= link_to 'Companies', companies_path(), if current_page?
(:controller=> 'Companies') :id=>"current" end%>

I have seen the rails widget s plugin for tabbed interface and it just
seems as too much code for something so simple.
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-11 07:21
(Received via mailing list)
Vinay wrote:
>
> seems as too much code for something so simple.
Creating your own tab link helper ought to ease things up:

def tab_link_to(name, options = {}, html_options = {})
  html_options.merge!({ :id => 'current' }) if current_page?(options)
  link_to name, options, html_options
end

Which you could then use in your view:

<% tab_link_to 'Companies', companies_path %>
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Vinay (Guest)
on 2008-10-11 11:05
(Received via mailing list)
@Erol,
Thank you so much! I have been letting this bug me for a couple of
days now and to see your simple solution working is an amazing pick-me-
up! :)
I think its the simplest tabbed interface code one could hope for.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-10-11 16:42
(Received via mailing list)
You might also want to look at link_to_unless_current (and perhaps its
source code) to see how you might change your helper (or abandon it
altogether in some cases).

-Rob
2dddec0f7717cae77ac6bceede6be5bc?d=identicon&s=25 Vinay (Guest)
on 2008-10-11 18:49
(Received via mailing list)
Thank you Rob.
I actually DID come across these helpers when i was searching around
for solutions. These though provide the link conditionally. If the
condition is not satisfied, the link itself does not exist, it only
renders text. But i need all the links to exist. And only the CSS id
to change conditionally. For which the code above worked like a charm.
Thanks again for your input!

On Oct 11, 7:41 pm, Rob Biedenharn <R...@AgileConsultingLLC.com>
613c0cc9552a2c880a902bb109c885b0?d=identicon&s=25 Youssef Chaker (Guest)
on 2008-10-12 00:44
(Received via mailing list)
This topic is locked and can not be replied to.