快速。我正在嘗試在創建對象時將參數推送到數據庫中。我有它的工作,如果我強制整數與下面的評論代碼一樣,但無論出於何種原因,我無法將它添加到數據庫中,我使用params [:subscription_id]方法。我可以看到服務器中的參數,但看不到它插入到證書表中。如果我強制在我的控制器中的整數它會通過,我可以看到subscriptions_id通過英寸param在Rails中沒有將數據推送到數據庫的問題
這是否有任何理由與我的關係可能是一個has_one?
Certificate Controller
class CertificatesController < ApplicationController
def new
@certificate = Certificate.new
end
def create
@certificate = Certificate.new(certificate_params)
@certificate.subscription_id = params[:subscription_id]
#@certificate.subscription_id = 1
if @certificate.save
flash[:success] = "The certificate has been uploaded"
redirect_to :back
else
render 'new'
end
end
private
def certificate_params
params.require(:certificate).permit(:document, :title, :subscription_id)
end
end
查看代碼:
<%= link_to "upload certificate", new_certificate_path(subscription_id: subscription.id) %>
模型
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :course
has_one :certificate
end
class Certificate < ActiveRecord::Base
belongs_to :subscription
has_attached_file :document
#validates_attachment :document, content_type: %w(application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)
do_not_validate_attachment_file_type :document
end
憑證申請表
<div class="row">
<div class="site-forms">
<div class="col-md-10">
<%= simple_form_for @certificate do |f| %>
<!-- <= f.input :course_img, as: :file, required: true, label: "Please upload a brand image for your course" %><br> -->
<span class="btn btn-default btn-file">
<i class="fa fa-cloud-upload fa-lg"></i> Upload Image
<%= f.input :document, as: :file, required: true, label: false %>
</span> Please upload Certificate as PDF <br><br>
<%= f.input :title, placeholder: "Course Title", required: true, label: "Course Title" %>
<%= f.button :submit, class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
這可能是一些愚蠢的我錯過,但我可以得到它的工作如果我強制中的整數在控制器中註釋掉代碼。我幾乎肯定參數代碼是正確的。謝謝。
添加日誌
Started POST "/certificates" for ::1 at 2017-05-23 20:40:30 +0100
Processing by CertificatesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6fV0xLOoy6ppNG7PYp37vkKqJnxU17nLPSK/mvbgH8k3s2LyaMEOJlekq5S0Ed4fpbNcqVe+cBsu5F38ACCmcg==", "certificate"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x007f954362c160 @tempfile=#<Tempfile:/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/RackMultipart20170523-5261-1n2mwfl.png>, @original_filename="duck.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"certificate[document]\"; filename=\"duck.png\"\r\nContent-Type: image/png\r\n">, "title"=>"Test"}, "commit"=>"Create Certificate"}
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/36846677e3a8f4c0b16d8bdf8ef1860820170523-5261-e90bny.png'
(0.1ms) begin transaction
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/36846677e3a8f4c0b16d8bdf8ef1860820170523-5261-1d907er.png'
SQL (0.3ms) INSERT INTO "certificates" ("document_file_name", "document_content_type", "document_file_size", "document_updated_at", "title", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["document_file_name", "duck.png"], ["document_content_type", "image/png"], ["document_file_size", 49114], ["document_updated_at", "2017-05-23 19:40:30.662715"], ["title", "Test"], ["created_at", "2017-05-23 19:40:30.683041"], ["updated_at", "2017-05-23 19:40:30.683041"]]
(0.7ms) commit transaction
Redirected to http://localhost:3000/certificates/new?subscription_id=3
Completed 302 Found in 36ms (ActiveRecord: 1.1ms)
Started GET "/certificates/new?subscription_id=3" for ::1 at 2017-05-23 20:40:30 +0100
Processing by CertificatesController#new as HTML
Parameters: {"subscription_id"=>"3"}
Rendered certificates/_form.html.erb (7.7ms)
Rendered certificates/new.html.erb within layouts/application (8.7ms)
Rendered shared/_message.html.erb (0.1ms)
Completed 200 OK in 95ms (Views: 94.3ms | ActiveRecord: 0.0ms)
現在沒有時間潛入它,稍後再看。但是在這種情況下,我的直覺立刻轉向「接受嵌套屬性」。這樣,證書控制器可以設置'@ certificate.subscription_id'表示的屬性。此外,相應的訂閱是否已經存在?還是需要'.build'命令? – Beartech
也請在嘗試保存時發佈您的Web服務器控制檯輸出。這樣我們可以看到傳遞給控制器的實際的'params []'。你的值很可能是'nil',所以這就是傳遞的內容,除非你在控制器中明確調用'= 1'。 – Beartech
http://guides.rubyonrails.org/form_helpers.html#building-complex-forms – Beartech