gpt4 book ai didi

ruby-on-rails - Ruby:开始事务,回滚事务

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:08 25 4
gpt4 key购买 nike

ruby 的新手,我遇到了一个小问题..需要一双新的眼睛

仅供引用:我没有使用脚手架,所以我可以理解在 ruby​​ 中构建的分步过程。

当我尝试添加新演出时,它没有保存在数据库中,我在服务器日志中看到以下错误...

Started POST "/gigs" for ::1 at 2017-03-06 17:34:34 +0300
Processing by GigsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lDSSVGuQDFlKwdkP8m1Gk4rzA4wG03wzpd5LmA19HZM7LPOLSrDBYGSsRRvwRZTbfVoe/HZmeGxBjRwVnZSKBw==", "gig"=>{"name"=>"Perry Dillard", "budget"=>"500", "duration"=>"4", "startdate"=>"19-Jan-1971", "description"=>"Reprehenderit, eligendi natus consequatur, dicta quae corporis voluptatem, ut pariatur. Consequatur excepteur natus eos, eos cum et eiusmod.", "location"=>"Dicta beatae culpa ut nostrud nihil voluptatem Eum", "open"=>"true", "hired"=>"1"}, "commit"=>"post it"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Redirected to http://localhost:3000/gigs
Completed 302 Found in 3ms (ActiveRecord: 0.1ms)


Started GET "/gigs" for ::1 at 2017-03-06 17:34:34 +0300
Processing by GigsController#index as HTML
Rendering gigs/index.html.erb within layouts/application
Rendered gigs/index.html.erb within layouts/application (0.6ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendered layouts/_nav.html.erb (1.6ms)
Completed 200 OK in 29ms (Views: 26.7ms | ActiveRecord: 0.2ms)

这是我的表格

<div class="container">
<h4 class ="center">Post an Internship Opportunity</h4>
<br>
<div class="col-sm-8">
<%= form_for @gig do |f| %>

<div class= "row">
<div class="col-sm-10">
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: "form-control input-lg" %>
<small class="healer block"> Data entry, writer, receptinist etc </small>
</div><!-- /.form-group -->
</div>

<div class="col-sm-3">
<div class="form-group">
<%= f.label :budget %>
<%= f.text_field :budget, class: "form-control" %>
<small class="healer block"> Free or min Kshs 5000 </small>
</div><!-- /.form-group -->
</div>
<div class="col-sm-4">
<div class="form-group">
<%= f.label :duration %>
<%= f.text_field :duration, class: "form-control" %>
<small class="healer block"> 2 weeks, 1 month, 3 months </small>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<%= f.label :startdate %>
<%= f.text_field :startdate, class: "form-control" %>
</div><!-- /.form-group -->
</div>
<div class="col-sm-10">
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
<small class="healer block"> What are the expectations and working hours, mentorship opportunities etc </small>
</div><!-- /.form-group -->
</div>
<div class="col-sm-10">
<div class="form-group">
<%= f.label :location %>
<%= f.text_field :location, class: "form-control " %>
<small class="healer block"> ignore if same location with your registered office </small>
</div><!-- /.form-group -->
</div>

<div class="col-sm-5">
<div class="form-group">
<%= f.label :open %>
<%= f.text_field :open, class: "form-control" %>
</div><!-- /.form-group -->
</div>
<div class="col-sm-5">
<div class="form-group">
<%= f.label :hired %>
<%= f.text_field :hired, class: "form-control" %>
</div><!-- /.form-group -->
</div>
<div class="col-sm-10">
<div class="form-group">
<button type="submit" class="btn btn-secondary btn-block"> <%= f.submit "post it" %> </button>
</div>
</div>
</div>
<% end %>
</div><!-- /.col-* -->

这是我的演出 Controller

class GigsController < ApplicationController

def index

end

def new
@gig = Gig.new
end

def create
@gig = Gig.new(gig_params)
@gig.save
redirect_to @gig
end


#private stuff
private

def gig_params
params.require(:gig).permit(:name, :description, :budget, :startdate, :duration, :location, :open, :hired )
end


end

觉得如果我添加我的架构可能会有帮助?

  create_table "gigs", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "budget"
t.date "startdate"
t.integer "duration"
t.string "location"
t.boolean "open", default: true
t.integer "hired"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.integer "user_id"
t.index ["category_id"], name: "index_gigs_on_category_id"
t.index ["user_id"], name: "index_gigs_on_user_id"
end

同时添加 Gig 模型

class Gig < ActiveRecord::Base
has_many :proposals
belongs_to :user
belongs_to :category
has_many :abilities
has_many :skills, through: :abilities
end

最佳答案

问题似乎出在验证中,您可以通过以下方式检查:

首先,转到您的 Rails 应用程序目录并执行:

rails console

接下来,创建一个新的演出实例:

g = Gig.new(name: "Perry Dillard", budget: "500") #Add all the fields

测试是否有任何验证失败

g.valid?

如果此返回 false,则您有一些正在停止保存的验证。看看是什么问题:

g.errors.messages

这应该会给您验证消息。

关于ruby-on-rails - Ruby:开始事务,回滚事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42628309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com