我有2个型号。类别和大小。我有嵌套的表格,但不起作用。
问题。当我提交表格时。我得到以下错误。有谁知道如何解决这一问题?任何信息都会很棒。提前致谢。
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
这是表格
<%= 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 %>
分类控制器
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
尺寸控制器
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
答案 0 :(得分:1)
您已在数据库外部进行了一些更改,并且没有写出这些更改,这是您database
被锁定的原因。
单击Write Changes
按钮,从sqlite3的GUI保存所有更改。然后运行rake
命令。它会工作...... !!!