Mantener el estado de mis rieles de navegación basados ​​en árboles

I have got a tree based navigation which I want across various actions of a controller. However when I move to another action by clicking on the link_to, my tree based structure collapses.

How can I maintain the state?

Here is the code if it is needed:-

.three.columns
  %ul(class = "continent_name")
    - @destinations.group_by(&:continent).each do |continent, ds_per_continent|
      %li=link_to continent, "#"
      %ul(class = "country_name")
        - ds_per_continent.group_by(&:country).each do |country, ds_per_country|
          %li=link_to country, "#"
          %ul(class = "city_name")
            - ds_per_country.each do |destination|
              %li=link_to destination.name, destination_path(destination)

When I go to destination path I want the country name to be visible instead of everything collapsed. How can this be done?

Código del controlador

class DestinationsController < ApplicationController
    before_filter :find_destination, :except => [:index]
    before_filter :all_destinations

    def index   
    end

    def show
        @photos = @destination.destination_photos.all
        cookies['destination_id'] = params[:id]
    end

    def photos_videos
        @photos = @destination.destination_photos.all
    end

    def topic_blog
        @topics = Topic.all
    end

    private

    def find_destination
        @destination = Destination.find(params[:id])
    end

    def all_destinations
        @destinations = Destination.all
        cookies['destination_id'] = params[:id]
    end
end

Código JS

$(document).ready(function(){
    $('.country_name').hide();
    $('.city_name').hide();
    $('li').click(function() {
        $(this).next('ul').toggle();
    });

    $('.city_name').click(function(){
    $('.destination').append();
    });
});

Modelo

class Destination < ActiveRecord::Base

    alias_attribute :city, :name
    validates :continent,   :presence   => true
    validates :country,     :presence   => true

end

Jquery

$('.country_name').hide();
    $('.city_name').hide();
    $('li').click(function() {
        $(this).next('ul').toggle();
    });

    $('.city_name').click(function(){
    $('.destination').append();
    });

preguntado el 10 de marzo de 12 a las 07:03

1 Respuestas

Utilice el cookies method in your navigation controller. The node to expand should be registered in the cookie, e.g.

cookies['country_id'] = params[:country_id]

And then on the JS side, you could extract the node easily by using a $.cookie función disponible en el complemento de cookies, p.ej:

$.cookie('country_id');

respondido 14 mar '12, 12:03

Yep, there is an error in the controller code - I fixed it, hope now it works. Also I don't know how you navigation controller code looks alike, so I am not sure, which variable should be memoized in the cookie. - Alejandro Pohl

I am updating my question with controller code please have a look at it - Desarrollador R

I don't see the correspondence between the controller and the view. Also the jQuery/JS code that you use to expand the tree nodes would also help. Do you use any plugins? - Alejandro Pohl

Updated the code with JS code and model code, I am not using any plugins. - Desarrollador R

When I hover the mouse on the the tree node which takes me to the action say "show" in the destination controller it reads like localhost:3000/destination/id - Desarrollador R

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.