【发布时间】:2015-09-13 07:29:15
【问题描述】:
在 rails 4.2.2 中,我使用 jstree 和 ancestry 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