Ruby:开始事务,回滚事务
新的ruby,我面临一个小问题……需要一双新鲜的眼睛
fyi:我没有使用脚手架,所以我可以理解在ruby中建造的一步一步的过程。
当我尝试添加新的gig时,它不会保存在数据库上,我在服务器日志中看到以下错误…
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)
这是我的表格
Post an Internship Opportunity
Data entry, writer, receptinist etc Free or min Kshs 5000 2 weeks, 1 month, 3 months What are the expectations and working hours, mentorship opportunities etc ignore if same location with your registered office
这是我的演出控制器
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
问题似乎在validation中,您可以通过执行以下操作来检查:
首先,转到rails app目录并执行:
rails console
接下来,创建一个新的gig实例:
g = Gig.new(name: "Perry Dillard", budget: "500") #Add all the fields
测试是否有任何validation失败
g.valid?
如果返回false,则有一些停止保存的validation。 问题是什么:
g.errors.messages
这应该给你validation的消息。
您正在尝试保存记录,但未通过某些validation。 您应该检查Ruby on Rails指南中的Active Record Validations 。 你也可以尝试打印@gig.errors
来看看什么是失败的。