Я использую гем Nested_Set в своем коде для сортировки категорий, подкатегорий и продуктов. Я пытаюсь ограничить глубину / уровень моего вложенного набора не глубже, чем 2. В настоящее время я получаю сообщение об ошибке
Nested Set Error undefined method `self_and_descendants' for #<ActiveRecord::Relation:0x52c4a30>
Я пытаюсь создать стиль меню типа Restraunt и пытаюсь сделать его перетаскиваемым.
Вот мой код:
Может кто-то просмотреть его и помочь мне понять эту ошибку? Спасибо
Category.rb
class Category < ActiveRecord::Base
acts_as_nested_set
acts_as_list :scope => :parent_id
has_many :products
scope :category, where("parent_id IS NULL")
scope :subcategories, where("parent_id IS NOT NULL")
scope :with_depth_below, lambda { |level| where(self.arel_table[:depth].lt(level)) }
end
categories_controller
class CategoriesController < ApplicationController
def new
@category = params[:id] ? Category.find(params[:id]).children.new : Category.new
@count = Category.count
end
def new_subcategory
@category = params[:id] ? Category.find(params[:id]).children.new : Category.new
@category_2_deep = Category.with_depth_below(2)
end
def create
@category = params[:id] ? Category.find(params[:id]).children.new(params[:category]) : Category.new(params[:category])
if @category.save
redirect_to products_path, :notice => "Category created! Woo Hoo!"
else
render "new"
end
end
def edit
@category = Category.find(params[:id])
end
def edit_subcategory
@category = Category.find(params[:id])
@category_2deep = Category.with_depth_below(2).arrange
end
def destroy
@category = Category.find(params[:id])
@category.destroy
flash[:notice] = "Category has been obliterated!"
redirect_to products_path
end
def update
@category = Category.find(params[:id])
if @category.update_attributes(params[:category])
flash[:notice] = "Changed it for ya!"
redirect_to products_path
else
flash[:alert] = "Category has not been updated."
render :action => "edit"
end
end
def show
@category = Category.find(params[:id])
end
def index
end
def sort
params[:category].each_with_index do |id, index|
Category.update_all({position: index+1}, {id: id})
end
end
end
routes.rb
Jensenlocksmithing::Application.routes.draw do
get "log_out" => "sessions#destroy", as: "log_out"
get "log_in" => "sessions#new", as: "log_in"
get "site/home"
get "site/about_us"
get "site/faq"
get "site/discounts"
get "site/services"
get "site/contact_us"
get "site/admin"
get "site/posts"
get "categories/new_subcategory"
get "categories/edit_subcategory"
resources :users
resources :sessions
resources :coupons
resources :monthly_posts
resources :categories do
collection { post :sort }
resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory]
end
resources :subcategories
resources :products
resources :reviews
resources :faqs do
collection { post :sort }
end
root to: 'site#home'
end
Категории / form.html.erb
<%= form_for(@category) do |f| %>
<p>
<%= f.label(:name) %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label(:parent_id) %>
<%= f.select :parent_id, nested_set_options(@category_2_deep, @category) {|i, level| "# {'-' * level if level < 1 } #{i.name if level < 1 }" } %>
</p>
<p>
<%= f.label(:position) %>
<%= f.select :position, 1..category_count %>
</p>
<p>
<%= f.submit("Submit") %>
</p>
<% end %>