2017-05-31 189 views
0

我有一個報價模型在我的Rails應用程序中有各種attr類型,其中一些正在發送到/由db保存,有些不是,我不明白爲什麼。請你能幫助我理解,謝謝。爲什麼有些模型屬性沒有保存在數據庫中?

quotes_controller.rb

class QuotesController < ApplicationController 

    def create 
    @quote = Quote.new(quote_params) 
    if @quote.save 
     redirect_to root_url, notice: 'Quote request created' 
    else 
     render :new 
    end 
    end 

private 

    def quote_params 
    params.require(:quote).permit(:gla, :prev_cover, :co_name, :postcode, :industry, :lives_overseas, 
            :scheme_start_date, :payment_frequency, :commision_level) 
    end 
end 

quote.rb模型

class Quote < ApplicationRecord 
     validates :gla, presence: { message: "Must be selected" } 

     enum industry:   [ :financial_services, :architect, :business_consultancy ] 
     enum payment_frequency: [ :annually, :monthly ] 
end 

schema.rb 

create_table "quotes", force: :cascade do |t| 
    t.boolean "prev_cover" 
    t.string "co_name" 
    t.integer "co_number" 
    t.string "postcode" 
    t.string "industry" 
    t.boolean "lives_overseas" 
    t.date  "scheme_start_date" 
    t.string "payment_frequency" 
    t.integer "commission_level" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.boolean "gla" 
    end 

軌控制檯:

Pry> Quote.last.attributes 
    Quote Load (0.4ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]] 
=> {"id"=>6, 
"prev_cover"=>true, 
"co_name"=>"test1", 
"co_number"=>nil, 
"postcode"=>"al1 1aa", 
"industry"=>nil, 
"lives_overseas"=>true, 
"scheme_start_date"=>Wed, 31 May 2017, 
"payment_frequency"=>nil, 
"commission_level"=>nil, 
"created_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00, 
"updated_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00, 
"gla"=>true} 

堆棧跟蹤:

Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:04:37 +0100 
Processing by QuotesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"ILAo0Bs9Wq9lrVPlM2e6+a1kioV9zbni9Uxd5Yt/QSLNY3aVWyJ4TsEUmXN62RWgbueHksr/yN6avwEm8v7bEQ==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"testing1", "co_number"=>"123456", "postcode"=>"al1 1aa", "industry"=>"", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"", "commission_level"=>"10"}, "commit"=>"Get quote"} 
Unpermitted parameters: co_number, commission_level 
    (0.1ms) BEGIN 
    SQL (0.2ms) INSERT INTO "quotes" ("prev_cover", "co_name", "postcode", "lives_overseas", "scheme_start_date", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["prev_cover", "t"], ["co_name", "testing1"], ["postcode", "al1 1aa"], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["created_at", "2017-05-31 20:04:37.489368"], ["updated_at", "2017-05-31 20:04:37.489368"], ["gla", "t"]] 
    (0.3ms) COMMIT 
Redirected to http://localhost:3000/ 
Completed 302 Found in 3ms (ActiveRecord: 0.6ms) 


Started GET "/" for 127.0.0.1 at 2017-05-31 21:04:37 +0100 
Processing by QuotesController#new as HTML 
    Rendering quotes/new.html.erb within layouts/application 
    Rendered quotes/new.html.erb within layouts/application (9.3ms) 
Completed 200 OK in 34ms (Views: 32.7ms | ActiveRecord: 0.0ms) 

從Rails控制檯。

[1] pry(main)> Quote.last 
    Quote Load (0.2ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]] 
=> #<Quote:0x007f951b14e918 
id: 7, 
prev_cover: true, 
co_name: "testing1", 
co_number: nil, 
postcode: "al1 1aa", 
industry: nil, 
lives_overseas: true, 
scheme_start_date: Wed, 31 May 2017, 
payment_frequency: nil, 
commission_level: nil, 
created_at: Wed, 31 May 2017 20:04:37 UTC +00:00, 
updated_at: Wed, 31 May 2017 20:04:37 UTC +00:00, 
gla: true> 

確定堆棧跟蹤和@toddmetheny幫助我理清丟失或錯字允許的attrs。現在只需要枚舉Quote.industriesQuote.payment_frequencies,其值不會被保存。

好的,所以代碼更改爲;

這從發送的形式ATTRS,但他們仍然沒有在數據庫生成,堆棧跟蹤:

Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:39:58 +0100 
Processing by QuotesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"rkgX7CwrEHS/KnqG1C77mYkCiCEOWGshTxMCsbtGPdjGiDP20J4ccrAgplAGuKrdJyhECRWrsmXI0Ee9GNa6Zw==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"halejulia", "co_number"=>"134532", "postcode"=>"al1 1aa", "industry"=>"financial_services", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"monthly", "commission_level"=>"10"}, "commit"=>"Get quote"} 
    (0.1ms) BEGIN 
    SQL (0.3ms) INSERT INTO "quotes" ("prev_cover", "co_name", "co_number", "postcode", "industry", "lives_overseas", "scheme_start_date", "payment_frequency", "commission_level", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["prev_cover", "t"], ["co_name", "halejulia"], ["co_number", 134532], ["postcode", "al1 1aa"], ["industry", 0], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["payment_frequency", 1], ["commission_level", 10], ["created_at", "2017-05-31 20:39:58.957674"], ["updated_at", "2017-05-31 20:39:58.957674"], ["gla", "t"]] 
    (0.3ms) COMMIT 
Redirected to http://localhost:3000/ 
Completed 302 Found in 3ms (ActiveRecord: 0.7ms) 

軌控制檯:

Quote.last.payment_frequency 
    Quote Load (0.4ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]] 
=> nil 

所以仍然是PARAMS AREN在數據庫中沒有被持久化,他們可以參數化!有任何想法嗎?

奇怪的是,一個psql select * from ..查詢顯示值已被保存,但Rails控制檯; Quote.last.payment_frequency返回零?

啊哈,我看到,對於枚舉列類型應該是整數,但它是在我的模型中的字符串,這可能是問題嗎?

enum'd attrs的數據類型已更改爲整數,並且所有行爲與預期相同。

+0

更新回答下面 – toddmetheny

回答

1

發佈堆棧跟蹤。 co_number未在允許的參數中列入白名單。所以這至少是該領域的一部分問題。其他人......但發佈您在日誌中實際看到的內容,以便我們可以看到表單中傳遞的內容。堆棧跟蹤中還會有消息提供有關爲什麼這些值未保存的線索。

更新:堆棧跟蹤列表(2個)未經許可參數:co_numbercommission_level(你有一個錯字允許的佣金水平,co_number是不存在的)

一對夫婦的東西具有空值,太...像payment_frequencyindustry ...我會深入探討爲什麼這些東西如果不應該是空白的。表單是否有這些東西的價值?他們沒有被通過。這似乎解釋了你的無價值的其餘部分。

+1

這裏有三個問題1)一個錯字和缺少允許的參數,2)我寫的地圖和代碼。人性化枚舉哈希不是重現原始枚舉鍵映射到枚舉值。 3)數據庫中的enum'd attr數據類型是字符串,並且需要爲整數。 – jbk

相關問題