User is not able to make root of tree in acts_as_tree in rails 5.0

196 Views Asked by At

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 %>
0

There are 0 best solutions below