Ruby/Rails:无法让控制器将新值插入数据库

发布于 2024-12-29 04:04:54 字数 2718 浏览 0 评论 0原文

我是 Ruby 和 Ruby on Rails 的新手。

我正在制作一个示例应用程序(不使用脚手架)以将一行插入表中。表有 2 列,“名称”和“描述”(以及 3 个魔法列)。

在我看来,我有一些看起来像这样的东西:

<h1>New Location</h1>
    <%= form_for(@location) do |l| %>
    <div class="field">
        <%= l.label :name %><br />
        <%= l.text_field :name %>
    </div>
    <div class="field">
        <%= l.label :description %><br />
        <%= l.text_field :description %>
    </div>
    <div class="actions">
         <%= l.submit "Add" %>
    </div>

控制器:

class LocationsController < ApplicationController
    def new
        @location = Location.new
    end

    def create
            @location = Location.new(params[:location])
            @location.save #fails on this line
            redirect_to @location
    end
end

模型代码:

class Location < ActiveRecord::Base
    attr_accessible :name, :description
    has_many :foods
end

这是控制台打印出来的内容:

Started POST "/locations" for 127.0.0.1 at Wed Jan 25 17:09:22 -0500 2012
Processing by LocationsController#create as HTML
Parameters: {"commit"=>"Add", "location"=>{"name"=>"name", "description"=>"description"}, "authenticity_token"=>"7rT1/3qSn/Kz1cGg8iTFCKbkO0W/L7ETT+lNcSSN6FI=", "utf8"=>"\342\234\223"} (0.1ms)  begin transaction
SQL (5081.3ms)  INSERT INTO "locations" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Wed, 25 Jan 2012 22:09:22 UTC +00:00], ["description", "description"], ["name", "name"], ["updated_at", Wed, 25 Jan 2012 22:09:22 UTC +00:00]]
SQLite3::BusyException: database is locked: INSERT INTO "locations" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)
(0.2ms)  rollback transaction
SQLite3::BusyException: cannot rollback transaction - SQL statements in progress: rollback transaction 
Completed 500 Internal Server Error in 5084ms

ActiveRecord::StatementInvalid (SQLite3::BusyException: cannot rollback transaction - SQL statements in progress: rollback transaction): app/controllers/locations_controller.rb:8:in `create'

Rendered /Library/Ruby/Gems/1.8/gems/actionpack 3.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack 3.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack 3.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.9ms)

tl;dr: 调用 .save 失败并返回 SQLite3::SQLException: 无法在事务中启动事务:开始交易

I'm new to Ruby and Ruby on Rails.

I'm making a sample app (not using scaffolding) to insert a row into a table. Table has 2 columns, "name" and "description" (along with the 3 magic columns).

In my view, I have something that looks like this:

<h1>New Location</h1>
    <%= form_for(@location) do |l| %>
    <div class="field">
        <%= l.label :name %><br />
        <%= l.text_field :name %>
    </div>
    <div class="field">
        <%= l.label :description %><br />
        <%= l.text_field :description %>
    </div>
    <div class="actions">
         <%= l.submit "Add" %>
    </div>

And controller:

class LocationsController < ApplicationController
    def new
        @location = Location.new
    end

    def create
            @location = Location.new(params[:location])
            @location.save #fails on this line
            redirect_to @location
    end
end

Model code:

class Location < ActiveRecord::Base
    attr_accessible :name, :description
    has_many :foods
end

This is what the console prints out:

Started POST "/locations" for 127.0.0.1 at Wed Jan 25 17:09:22 -0500 2012
Processing by LocationsController#create as HTML
Parameters: {"commit"=>"Add", "location"=>{"name"=>"name", "description"=>"description"}, "authenticity_token"=>"7rT1/3qSn/Kz1cGg8iTFCKbkO0W/L7ETT+lNcSSN6FI=", "utf8"=>"\342\234\223"} (0.1ms)  begin transaction
SQL (5081.3ms)  INSERT INTO "locations" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Wed, 25 Jan 2012 22:09:22 UTC +00:00], ["description", "description"], ["name", "name"], ["updated_at", Wed, 25 Jan 2012 22:09:22 UTC +00:00]]
SQLite3::BusyException: database is locked: INSERT INTO "locations" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)
(0.2ms)  rollback transaction
SQLite3::BusyException: cannot rollback transaction - SQL statements in progress: rollback transaction 
Completed 500 Internal Server Error in 5084ms

ActiveRecord::StatementInvalid (SQLite3::BusyException: cannot rollback transaction - SQL statements in progress: rollback transaction): app/controllers/locations_controller.rb:8:in `create'

Rendered /Library/Ruby/Gems/1.8/gems/actionpack 3.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack 3.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack 3.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.9ms)

tl;dr: calling .save fails and returns SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-01-05 04:04:54

这个错误暴露了这一点:

SQLite3::BusyException: database is locked

可能有另一个正在运行的进程正在同时使用该数据库。

This error gives it away:

SQLite3::BusyException: database is locked

There's probably another process running that is using the database at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文