我有一个基于树的导航,我希望跨越控制器的各种动作。但是,当我通过单击link_to移动到另一个操作时,基于树的结构就会崩溃。
我怎样才能维持这个州?
这是代码,如果需要的话:
.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)当我进入目的地路径时,我希望国家名称是可见的,而不是所有崩溃的东西。这是如何做到的呢?
控制器代码
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
endJS代码
$(document).ready(function(){
$('.country_name').hide();
$('.city_name').hide();
$('li').click(function() {
$(this).next('ul').toggle();
});
$('.city_name').click(function(){
$('.destination').append();
});
});模型
class Destination < ActiveRecord::Base
alias_attribute :city, :name
validates :continent, :presence => true
validates :country, :presence => true
endJquery
$('.country_name').hide();
$('.city_name').hide();
$('li').click(function() {
$(this).next('ul').toggle();
});
$('.city_name').click(function(){
$('.destination').append();
});发布于 2012-03-12 10:13:54
在导航控制器中使用cookies方法。要展开的节点应该在cookie中注册。
cookies['country_id'] = params[:country_id]然后,在JS端,可以使用$.cookie函数轻松地提取节点,例如:
$.cookie('country_id');https://stackoverflow.com/questions/9644793
复制相似问题