SreRoR SreRoR - 6 months ago 117
Ruby Question

How to display categories and subcategories in ruby on rails

I want to display

categories
and
subcategories
in ruby on rails

I am getting this error

enter image description here

This is my
categories
table, from which I want to display categories and subcategories.

enter image description here

My code in
category.rb


class Category < ActiveRecord::Base
belongs_to :category
has_many :children, :dependent => :destroy, :class_name => 'Category'
end


categories_controller.rb

class CategoriesController < ApplicationController
def index
@categories = Category.all
end
def new
@category = Category.new
end
def edit
@category = Category.find(params[:id])
end
def create
@category = Category.new(params[:category].permit!)
if @category.save
redirect_to categories_url
else
render :new
end
end
def update
@category = Category.find(params[:id])
if @category.update_attributes(params[:category].permit!)
redirect_to categories_url
else
render :edit
end
end
def destroy
Category.destroy(params[:id])
redirect_to categories_url
end
end


view/categories/index.html.erb

<%= form_for @category do |f| %>

<%= f.text_field :name %>
<%= f.select :category_id, options_from_collection_for_select(Category.all, :id, :name, @category.category_id), :include_blank => true %>
<%= f.submit %>
<% end %>`


error is

enter image description here

routes.rb

Rails.application.routes.draw do
devise_for :organisations, :controllers => {:registrations => "organisations/registrations"}
devise_for :coaches, :controllers => {:registrations => "coaches/registrations"}
devise_for :students, :controllers => {:registrations => "students/registrations"}
devise_for :admins
root to: "home#index"
devise_for :users, path: 'users', path_names: {sign_in: 'sign-in', sign_out: 'sign-out', confirmation: 'verification'}
namespace :coaches do
resources :study_materials
end
namespace :coaches do
resources :programmes
end
namespace :admin do
resources :coaches
end
get 'programmes', to: 'programmes#index', as: 'programmes'
get 'programmes/*hierarchy/:program_slug', to: 'programmes#show', as: 'programme'
get 'programmes/*hierarchy', to: 'programmes#category', as: 'categories'
get 'start-coaching', to: 'home#start_coaching'
resources :categories
end

Answer

For your nil class you have to add this in your def index method

def index
  @categories = Category.where("parent_id IS NULL")
  @category = Category.new
end

def get_subscategories
  @subscategories = Category.where(:parent_id => params[:parent_id])
  render :partial => "subscategories", :object => @subscategories
end

index.html

<%= select_tag "parent_category", options_from_collection_for_select(@categories, "id", "name"), :prompt => "Select a parent category", :onchange => "update_subscategories_div(this.value)" %>
<div id="versionsDiv">
</div>

_subscategories.html.erb

<%= f.select :parent_id, options_from_collection_for_select(@subscategories, "id", "name"), :prompt => "Select a parent category" %>

routes.rb

match "/get_subscategories" => "categories#get_subscategories"

application.js

function update_subscategories_div(parent_id) {  
  jQuery.ajax({
    url: "/get_subscategories",
    type: "GET",
    data: {"parent_id" : parent_id},
    dataType: "html"
    success: function(data) {
      jQuery("#versionsDiv").html(data);
    }
  });
}

This is reference articles for you how-categories-sub-categories-in-tree-hierarchy-inside-a-dropdown