Forum: Ruby on Rails Is there any better way to do this?

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.
Fresh M. (Guest)
on 2009-01-03 22:04
Is there any better way to do this?

<li <%= ('class="current"') if controller.controller_name == 'users' &&
controller.action_name == 'login' %>>
Bill D. (Guest)
on 2009-01-03 22:37
Gi Ga wrote:
> Is there any better way to do this?
>
> <li <%= ('class="current"') if controller.controller_name == 'users' &&
> controller.action_name == 'login' %>>

Here's my take:

<%= content_tag :li, (:class => "current" unless current_page?
(:controller => 'users', :action => 'login')) %>

I've not actually tried it though.

Bill
Bill D. (Guest)
on 2009-01-03 22:40
Bill D. wrote:
> Gi Ga wrote:
>> Is there any better way to do this?
>>
>> <li <%= ('class="current"') if controller.controller_name == 'users' &&
>> controller.action_name == 'login' %>>
>
> Here's my take:
>
> <%= content_tag :li, (:class => "current" unless current_page?
> (:controller => 'users', :action => 'login')) %>
>
> I've not actually tried it though.
>
> Bill

Now that I look again, I think the "unless" needs to be "if".

Bill
Tony (Guest)
on 2009-01-04 09:57
(Received via mailing list)
<li<% if controller.controller_name == 'users' &&
controller.action_name == 'login' %> class="current"<% end %>>

Probably safe syntax. I usually don't like to print things inside of
operations. Plus, with this, you won't get any unnecessary whitespace.

But, there is no right way. As long as that Ruby interpreter does it's
job and doesn't blow up in your face!
Phlip (Guest)
on 2009-01-04 16:14
(Received via mailing list)
> <li <%= ('class="current"') if controller.controller_name == 'users' &&
> controller.action_name == 'login' %>>

First, move "if controller.controller_name == 'users' &&
controller.action_name
== 'login'" into a helper and give it a name, such as login_page_class.
It
should return nil on the wrong page and 'current' on the right page.

Next, use content_tag to build the entire li. Then if :class =>
login_page_class
contains the nil, the class="" itself will go away in the rendered HTML.

(And put all this under test, because it's logic that you want to keep
alive as
you upgrade this site!)

--
   Phlip
This topic is locked and can not be replied to.