nobosh
2010-Oct-07 04:16 UTC
[Rails] Rails 3 - Referencing the URL to determine if a Nav Item is class=“selected”
The app can have the following URLs: <li> /projects/ </li> <li> /projects/3 </li> <li> /projects/3/photos </li> <li> /projects/3/photos/312 </li> I''d like to know how in Rails 3 I can look at the current URL and know which of the lines above is currently active, so I can add a class="selected" Additional exmaple.. If the user''s browser is on: /projects And the Nav looks like Projects - Photos - Files Projects is Active but if the user''s browser is on: /files Projects is active and Files is active (they both apply). Ideas? thanks -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Joshua Martin
2010-Oct-07 12:18 UTC
Re: [Rails] Rails 3 - Referencing the URL to determine if a Nav Item is class=“selected”
I use this method: Give each li an id (i.e. app_nav_li_projects, app_nav_li_files) Set @li_to_select in your controller or controller method to the li id you want to select Use JQuery on the page to add the selected class to the li id of the @li_to_select My JQuery looks like this: <script type="text/javascript"> // Adds the current class to the requested app nav menu $(document).ready(function(){ $("li#<%= @li_to_select%>").addClass("selected"); }); </script> Note that your JQuery has to be in the page so the @li_to_select is inserted correctly. On Thu, Oct 7, 2010 at 12:16 AM, nobosh <bhellman1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The app can have the following URLs: > > <li> /projects/ </li> > <li> /projects/3 </li> > <li> /projects/3/photos </li> > <li> /projects/3/photos/312 </li> > > I''d like to know how in Rails 3 I can look at the current URL and know > which of the lines above is currently active, so I can add a > class="selected" > > Additional exmaple.. > > If the user''s browser is on: /projects > > And the Nav looks like > > Projects > - Photos > - Files > > Projects is Active > > but if the user''s browser is on: /files > > Projects is active > > and Files is active (they both apply). > > > Ideas? thanks > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- _________________________________ Joshua S. Martin CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s), contains information that may be confidential, protected by the attorney client or other legal privileges, and or proprietary non public information. If you are not an intended recipient of this message or an authorized assistant to an intended recipient, please notify the sender by replying to this message and then delete it from your system. Use, dissemination, distribution, or reproduction of this message and or any of its attachments (if any) by unintended recipients is not authorized and may be unlawful. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Oct-07 13:15 UTC
[Rails] Re: Rails 3 - Referencing the URL to determi ne if a Nav Item is class=“selected”
Joshua Martin wrote:> I use this method: > > Give each li an id (i.e. app_nav_li_projects, app_nav_li_files) > > Set @li_to_select in your controller or controller method to the li id > you > want to select > > Use JQuery on the page to add the selected class to the li id of the > @li_to_select > > My JQuery looks like this: > > > <script type="text/javascript"> > > // Adds the current class to the requested app nav menu > $(document).ready(function(){ > $("li#<%= @li_to_select%>").addClass("selected"); > }); > > </script>Terrible. As I''ve said numerous times on this forum, dynamic JS -- and inline JS even more so -- are problematic from a design standpoint and should always be avoided. What you should be doing here is using an external JS file to which you pass the ID by a method such as having it look in the DOM. But I''m not sure this is the right approach. Rails provides a bunch of helpers such as current_page? and link_to_if_current_page, so it seems to me that you should be able to do this on the server side. [...]> Joshua S. Martin > > >Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
nobosh
2010-Oct-07 16:04 UTC
[Rails] Re: Rails 3 - Referencing the URL to determi ne if a Nav Item is class=“selected”
Thanks all. I have no intention of wanting to do this client-side with JS... Only Server Side... Any suggestions? Seems like this is something every RAils app needs solved? On Oct 7, 6:15 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Joshua Martin wrote: > > I use this method: > > > Give each li an id (i.e. app_nav_li_projects, app_nav_li_files) > > > Set @li_to_select in your controller or controller method to the li id > > you > > want to select > > > Use JQuery on the page to add the selected class to the li id of the > > @li_to_select > > > My JQuery looks like this: > > > <script type="text/javascript"> > > > // Adds the current class to the requested app nav menu > > $(document).ready(function(){ > > $("li#<%= @li_to_select%>").addClass("selected"); > > }); > > > </script> > > Terrible. As I''ve said numerous times on this forum, dynamic JS -- and > inline JS even more so -- are problematic from a design standpoint and > should always be avoided. What you should be doing here is using an > external JS file to which you pass the ID by a method such as having it > look in the DOM. > > But I''m not sure this is the right approach. Rails provides a bunch of > helpers such as current_page? and link_to_if_current_page, so it seems > to me that you should be able to do this on the server side. > > [...] > > > Joshua S. Martin > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Sent from my iPhone > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Oct-07 16:19 UTC
[Rails] Re: Rails 3 - Referencing the URL to determi ne if a Nav Item is class=“selected”
Please quote when replying. nobosh wrote:> Thanks all. I have no intention of wanting to do this client-side with > JS... Only Server Side... Any suggestions? Seems like this is > something every RAils app needs solved?Yes. Read my previous post, where I explained how to do it server-side. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.