2

I am using acts_as_tree for creating tree structure of folder(model) in my rails app.But after adding acts_as_tree and when i try to make a root folder a error comes that parent must exist, indicating parent_id can't be nil .

folder.rb :

class Folder < ApplicationRecord
  belongs_to :user
  has_many :assets, :dependent => :destroy
  acts_as_tree
end

folder:

class CreateFolders < ActiveRecord::Migration[5.0]
def change
create_table :folders do |t|
  t.string :name
  t.integer :parent_id
  t.integer :user_id

  t.timestamps
end
add_index :folders, :parent_id
  add_index :folders, :user_id
end
end

_form.html.erb :

<%= form_for(folder) do |f| %>
<% if folder.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(folder.errors.count, "error") %> prohibited this 
  folder from being saved:</h2>

  <ul>
  <% folder.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
  <%= f.hidden_field :parent_id,:include_blank=>true %>
</div>

<div class="actions">
  <%= f.submit %>
</div>
<% end %>
Ankush Malik
  • 150
  • 2
  • 11
  • i just moved from acts_as_tree to ancestry and everything is working fine. May be there is a problem with acts_as_tree gem? – Ankush Malik Aug 22 '17 at 04:08
  • See https://github.com/amerine/acts_as_tree. you have no code to make tree relationshiop. – artgb Aug 22 '17 at 08:08

0 Answers0