2017-03-06 87 views
1

新红宝石,我面临一个小问题..交易需要一双崭新的眼睛FYI红宝石:开始交易,回滚

的:我不使用支架,这样我可以理解一步一步红宝石建筑的过程。

当我尝试添加一个新的演出,它不保存在数据库中,我看到我的服务器日志下面的错误...

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"> 
 
\t <h4 class ="center">Post an Internship Opportunity</h4> 
 
\t <br> 
 
    <div class="col-sm-8"> 
 
<%= form_for @gig do |f| %> 
 
    
 
    \t <div class= "row"> 
 
    \t \t <div class="col-sm-10"> 
 
    \t \t \t <div class="form-group"> 
 
    \t \t \t \t <%= f.label :name %> 
 
    \t \t \t \t <%= f.text_field :name, autofocus: true, class: "form-control input-lg" %> 
 
    \t \t \t \t <small class="healer block"> Data entry, writer, receptinist etc </small> 
 
    \t \t \t </div><!-- /.form-group --> 
 
    \t \t </div> 
 

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

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

这是我演出控制器

class GigsController < ApplicationController 
 

 
\t def index 
 
\t \t 
 
\t end 
 

 
\t def new 
 
\t \t @gig = Gig.new 
 
\t end 
 

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

 
\t 
 
#private stuff 
 
\t private 
 

 
\t def gig_params 
 
\t \t params.require(:gig).permit(:name, :description, :budget, :startdate, :duration, :location, :open, :hired) 
 
\t 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

添加千兆模型以及

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

+1

很可能你有一些失败的验证 - 请张贴'Gig'型号代码。 – eugen

+0

嗨欧根,我添加了演出模型 – samgichuru

+2

在GigsController#create中将您的代码从'@gig.save'更改为'@ gig.save!'。您将在控制台输出中看到验证错误。 –

回答

6

的问题似乎是在验证,你可以通过做检查:

弗里斯特,去你的Rails应用程序的目录,然后执行:

rails console 

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

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

测试,如果任何验证失败

g.valid? 

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

g.errors.messages 

这应该给你的验证信息。

+0

使用Gig#save可以实现同样的效果!而不是Gig#保存。演出#保存!将执行验证并在失败验证时引发ActiveRecord :: RecordInvalid。有关更多详细信息,请参阅http://apidock.com/rails/v2.3.8/ActiveRecord/Base/save%21。 –

+0

感谢您的信息,但我正在使用'g.valid?' –

+0

@ alejandor-montilla对不起,我感到困惑:您的解决方案完全正常,我只想指出hashbanged'save'-方法会自动引发验证错误 –

2

您试图保存记录,但失败了一些验证。 你应该检查Ruby on Rails指南Active Record Validations。 你也可以尝试打印@gig.errors看看有什么失败。

+0

谢谢,我会读这个! – samgichuru

+0

查看您的模型定义后,很可能发生错误是由于空关联造成的。 Ruby on Rails 5在默认情况下对al关联进行验证验证。请参阅[belongs_to](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to)'optional'参数 –