Ruby:开始事务,回滚事务

时间:2017-03-06 14:36:56

标签: ruby-on-rails ruby

新的红宝石,我面临一个小问题......需要一双新鲜的眼睛

fyi:我没有使用脚手架,这样我就能理解在红宝石中建造的一步一步的过程。

当我尝试添加新的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)




这是我的表格



<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-* -->
&#13;
&#13;
&#13;

这是我的演出控制器

&#13;
&#13;
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
&#13;
&#13;
&#13;

如果我添加架构,感觉会有所帮助吗?

&#13;
&#13;
  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
&#13;
&#13;
&#13;

添加Gig模型

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

1 个答案:

答案 0 :(得分:2)

您正在尝试保存记录,但未通过某些验证。 您应该查看Active Record Validations的Ruby on Rails指南。 您也可以尝试打印@gig.errors以查看最新情况。