[ad_1]
I am using rails 5.1 and am trying to display some data in a tree format using jstree. It's working by my sorting and grouping is not correct and would like to know how to get this right. So far nothing I have tried works.
Here is a screenshot of what it looks like now.
I have it set up so Cut Sheets is called category1 and Card Access Peripherals is category2. So I want to first group by category1, then category2 so I have one folder for cut sheets, one folder for card access peripherals and many documents under that.
Here is my documents model:
class Document < ApplicationRecord
belongs_to :user
has_many :doccategories
has_many :docsubcategories
include FileUploader[:file]
end
Here is the documents controller:
class DocumentsController < ApplicationController
before_action :set_document, only: [:show, :edit, :update, :destroy]
# GET /documents
# GET /documents.json
def index
@documents = Document.all
@document = Document.new
@category = Doccategory.all
@subcategory = Docsubcategory.all
end
# GET /documents/1
# GET /documents/1.json
def show
end
# GET /documents/new
def new
@document = Document.new
@category = Doccategory.all
@subcategory = Docsubcategory.all
end
# GET /documents/1/edit
def edit
end
# POST /documents
# POST /documents.json
def create
@document = Document.new(document_params)
@document.user_id = current_user.id
respond_to do |format|
if @document.save
format.html redirect_to @document, notice: 'Document was successfully created.'
format.json render :show, status: :created, location: @document
else
format.html render :new
format.json render json: @document.errors, status: :unprocessable_entity
end
end
end
# PATCH/PUT /documents/1
# PATCH/PUT /documents/1.json
def update
respond_to do |format|
if @document.update(document_params)
format.html redirect_to @document, notice: 'Document was successfully updated.'
format.js
format.json render :show, status: :ok, location: @document
else
format.html render :edit
format.json render json: @document.errors, status: :unprocessable_entity
end
end
end
# DELETE /documents/1
# DELETE /documents/1.json
def destroy
@document.destroy
respond_to do |format|
format.html redirect_to documents_url, notice: 'Document was successfully destroyed.'
format.json head :no_content
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_document
@document = Document.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def document_params
params.require(:document).permit(:file, :title, :category1, :category2)
end
end
Here is where I build the tree in index.html.erb
<div id="jstree1">
<% @documents.each do |document| %>
<ul>
<li class="jstree-closed">
<%= document.category1 %>
<ul>
<li>
<%= document.category2 %>
<ul>
<li data-jstree='"type":"css"'><%= link_to (document.title + "-" + document.file.original_filename), document.file.url %></li>
</ul>
</li>
</ul>
</li>
</ul>
<% end %>
</div>
Any help is appreciated!
[ad_2]
لینک منبع