【问题标题】:Rails 4.2.2 - How to create jstree dynamically?Rails 4.2.2 - 如何动态创建 jstree?
【发布时间】:2015-09-13 07:29:15
【问题描述】:

在 rails 4.2.2 中,我使用 jstreeancestry gems 作为文件结构。现在我能够生成祖先数据,但我无法在 jstree 函数中访问它。我从this tutorial 转介。当我使用相同的示例时,出现如下错误,

NoMethodError (undefined method `category_path' for #<Module:0x00000004b1cd40>):
app/models/category.rb:12:in `block in build_display_ancestry'
app/models/category.rb:7:in `each'
app/models/category.rb:7:in `build_display_ancestry'
app/models/category.rb:21:in `viewed_ancestry'

型号代码是,

class Category < ActiveRecord::Base
  has_ancestry
  belongs_to :user
  belongs_to :asset

  def self.build_display_ancestry(category_hierarchy, tailored_hierarchy_data = [])
    category_hierarchy.each do |category_data|
      state = category_data['children'].empty? ? 'leaf' : 'open'
      custom_display_data = {
        :data => category_data['name'],
        :state => state,
        :metadata => { href: Rails.application.routes.url_helpers.category_path(category_data['id']) },
        :children =>  build_display_ancestry(category_data['children'], [])
      }
      tailored_hierarchy_data << custom_display_data
    end
    tailored_hierarchy_data
  end

  def self.viewed_ancestry
    build_display_ancestry(self.arrange_serializable, [])
  end

end

控制器代码是,

class Users::CategoriesController < ApplicationController
  def index
  end

  def view_ancestry
    render json: { data: 'Categories', state: 'open', metadata: { href: categories_path }, children: Category.viewed_ancestry }
  end
end

查看是

<div id="category-hierarchy"></div>

<script type="text/javascript">
  $(document).ready(function(){
  $("#category-hierarchy").jstree({
    "json_data": {
        "ajax" : {
            "url" : $("#category-hierarchy").attr('data-path'),
            "data" : function (n) {
                return { id : n.attr ? n.attr("id") : 0 };
            }
        }
    },
    plugins : ["themes", "json_data", "ui"],
    themes : {"theme": "default", "icons":false, "dots": true, "open":true},
    core: {"animation": 100}
   }).bind("select_node.jstree", function(e, data) {
    if (jQuery.data(data.rslt.obj[0], "href")) {
        window.location = jQuery.data(data.rslt.obj[0], "href");
    }
  })
 });
</script>

路线是,

get 'users/categories/index' => 'users/categories#index', :as=> :categories
get 'users/categories/view_ancestry' => 'users/categories#view_ancestry', :as=> :users_categories_view_ancestry

这里,我是否需要创建category_path 路由?如果是,应该采取什么行动?请帮我解决这个问题,并给我一些示例(带有完整代码)链接以供参考。

【问题讨论】:

    标签: jquery ruby-on-rails ruby jstree ancestry


    【解决方案1】:

    jsTree 改变了它的语法,很多,检查那里的网页。我能够通过(来自同一个教程:))通过将教程中的代码更改为:

    .jstree({ 'core' : {
        'data': {
                'url' : '<some url>/<JSONdatasource>',
                'data' : function (n) {
                    return { id : n.attr ? n.attr('id') : 0 };
                }
        }
      }
    })
    

    只需按照网页上的(所有)更改,虽然我必须说非常不方便。

    编辑。请注意,虽然一个问题(如所问)确实存在未定义方法的问题,但我回答时的根本/潜在问题是 jsTree 节点的 JSON 数据的语法已更改。

    【讨论】:

      【解决方案2】:

      您可以在您的 rails 应用程序目录中输入rake routes | grep categor,使用您的终端查看哪些路线可用。

      您可能应该使用 rails 资源路由:

      http://guides.rubyonrails.org/routing.html

      namespace :users do
        resources :categories do
          collection do
            get :view_ancestry
          end
        end
      end
      

      【讨论】:

      • 更改后,错误已解决,但jstree结构未加载。请发给我分类控制器、视图、模型的完整代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多