I have 2 models. Category, and Size. I have nested form that isn't working.
PROBLEM. When i submit the form. I get the below error. Does anyone know how to fix this? Any information would be great. Thanks in advance.
ActiveRecord::StatementInvalid (SQLite3::BusyException: database is locked: commit transaction):
app/controllers/categories_controller.rb:28:in `create'
Category.rb
class Category < ActiveRecord::Base
has_ancestry
has_many :products
has_many :sizes
validates :name, presence: true, length: { maximum: 20 }, uniqueness: true
accepts_nested_attributes_for :sizes
end
Size.rb
class Size < ActiveRecord::Base
validates :title, presence: true, length: { maximum: 15 }
validates :title, uniqueness: true
belongs_to :category
end
Here is the form
<%= simple_form_for(@category) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.collection_select :parent_id, Category.order(:name), :id, :name, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
<%= f.simple_fields_for :sizes do |s| %>
<%= s.input :title %>
<% end %>
<div class="form-actions"><%= f.button :submit %></div>
</div>
<% end %>
Categories Controller
class CategoriesController < ApplicationController
def create
@category = Category.new(category_params)
if @category.save
redirect_to @category
flash[:success] = "You have created a new category"
else
flash[:danger] = "Your category didn't save"
render "new"
end
end
def category_params
binding.pry
params.require(:category).permit(:name, :parent_id, size_ids: [], sizes_attributes: [:title])
end
end
Sizes Controller
class SizesController < ApplicationController
before_action :logged_in_user, only: [:create, :index, :destroy, :update]
before_action :admin_user, only: [:create, :index, :destroy, :update]
def create
@size = Size.create(size_params)
end
def index
@sizes = Size.all
end
def destroy
Size.find(params[:id]).destroy
end
def update
@size.update_attributes(size_params)
end
private
def size_params
params.require(:size).permit(:title, :category_id)
end
end
Aucun commentaire:
Enregistrer un commentaire