Maintaining the state of my tree-based navigation guides

I have tree navigation that I want to use for different controller actions. However, when I move on to another action by clicking the link_to link, my tree structure collapses.

How can I maintain my condition?

Here is the code if necessary: ​​-

.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 the destination, I want the name of the country to be visible, and not everything collapsed. How can I do that?

Controller code

 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 

JS code

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

Model

 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(); }); 
+4
source share
1 answer

Use the cookies method in your navigation controller. The node extension for the extension must be registered in a cookie, for example

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

And then on the JS side, you can easily extract the node using the $.cookie function available in the cookie plugin , for example:

 $.cookie('country_id'); 
0
source

Source: https://habr.com/ru/post/1400705/


All Articles