在rails 4.2.2中,我使用jstree和ancestry gem作为文件结构。现在我可以生成祖先数据,但不能在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路由?如果是,应该采取什么行动?请帮助我解决这个问题,也给我一些例子(与完整的代码)链接参考。
发布于 2018-01-27 16:58:00
jsTree改变了它的语法,很多,在那里的网页上检查。我能够通过(来自同一教程:)这一步,将教程中的代码更改为:
.jstree({ 'core' : {
'data': {
'url' : '<some url>/<JSONdatasource>',
'data' : function (n) {
return { id : n.attr ? n.attr('id') : 0 };
}
}
}
})只需遵循网页上的(所有)更改,尽管我必须说这非常不方便。
编辑。注意,尽管一个问题(被问到的)确实存在未定义方法的问题,但在我回答这个问题时,根本/潜在问题是jsTree节点的JSON数据的语法发生了变化。
发布于 2015-09-13 19:35:01
您可以在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
endhttps://stackoverflow.com/questions/32547343
复制相似问题