views:

22

answers:

1

The app can have the following URLs:

  • /projects/
  • /projects/3
  • /projects/3/photos
  • /projects/3/photos/312
  • 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

    +1  A: 

    There is always access to params hash, so you can check params[:controller] or params[:action] anywhere in your views. There is always request.env['PATH_INFO'], where the complete URL is stored as well.

    As for the breadcrumb referenced in the second part of the question, I believe there is no general way of doing it. There is no solid background behind the relations between views. Site logic is basically constructed across the views, so there is no solid way of determining it.

    Since you always have Files underneath Project in this particular view, where is the problem? Can't you just highlight project if Files are selected?

    Edit:

    Implementation really depends on your design and if you need direct hints, you would have to paste a gist/paste link to your code. In general the idea is as follows:

    <% if params[:controller] == :files 
      <% b1class = "active" %>
      <% b2class = "active" %>
      <% b3class = "active" %>
    <% else %>
       ....
    <% end %>
    
    <div class=breadcrumb>
      <p class=<%= b1class> Project </p>
      <p class=<%= b2class> Photos </p>
      <p class=<%= b3class> Files </p>
    </div>
    
    mdrozdziel
    Right, I always want to highlight project if anything under project is in the URL, and and then if there is a sub item of project I want that item set as class=active as well. Would you mind showing an example given the URL above? (/projects/3/photos/312) how would I set projects and photos as active in the application.html.erb file? thxs
    AnApprentice
    I did an edit, but the question is too generic in my opinion. You have to either paste the code or be way more specific.
    mdrozdziel
    @mdrozdziel, I'll try to be clearer. So what you have above is great. But lets say we have a URL like: /projects/files/ Given the params[:controller] we'll only get back files, when what I want is to get back projects and files, so that I can set both projects and file with class active? Does that make more sense? Maybe if I notice a sub item being active I can infer that the partent is also active?
    AnApprentice
    I think just making all parents active makes most sense. You can always parse request.env['PATH_INFO'] if your logic is very different from standard breadcrumb.
    mdrozdziel