[英]Rails: How to display categories and subcategories in ruby on rails
I want to display categories
and subcategories
in ruby on rails我想在 ruby on rails 中显示
categories
和subcategories
I am getting this error我收到此错误
This is my categories
table, from which I want to display categories and subcategories.这是我的
categories
表,我想从中显示类别和子类别。
My code in category.rb
我在
category.rb
代码
class Category < ActiveRecord::Base
belongs_to :category
has_many :children, :dependent => :destroy, :class_name => 'Category'
end
categories_controller.rb category_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查看/类别/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错误是
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
For your nil class you have to add this in your def index
method对于您的 nil 类,您必须在
def index
方法中添加它
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索引.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 _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应用程序.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这是您的参考文章how-categories-sub-categories-in-tree-hierarchy-inside-a-dropdown
As you the form
in index.html.erb
, the index
method should contain @category = Category.new
作为
index.html.erb
的form
, index
方法应该包含@category = Category.new
def index
@categories = Category.all
@category = Category.new
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.