2017-03-06 60 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'參數 –