2017-05-22 65 views
0

快速。我正在嘗試在創建對象時將參數推送到數據庫中。我有它的工作,如果我強制整數與下面的評論代碼一樣,但無論出於何種原因,我無法將它添加到數據庫中,我使用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) 
+0

現在沒有時間潛入它,稍後再看。但是在這種情況下,我的直覺立刻轉向「接受嵌套屬性」。這樣,證書控制器可以設置'@ certificate.subscription_id'表示的屬性。此外,相應的訂閱是否已經存在?還是需要'.build'命令? – Beartech

+0

也請在嘗試保存時發佈您的Web服務器控制檯輸出。這樣我們可以看到傳遞給控制器​​的實際的'params []'。你的值很可能是'nil',所以這就是傳遞的內容,除非你在控制器中明確調用'= 1'。 – Beartech

+0

http://guides.rubyonrails.org/form_helpers.html#building-complex-forms – Beartech

回答

1

您需要將您的subscription_id設置爲隱藏字段,以便它可以在params散列中傳遞。

<%= f.hidden_field :subscription_id, :value => params[:subscription_id] %> 

這樣就可以設置其餘的參數。

+0

再次感謝Beartech,就這麼簡單。我通過嘗試在控制器中添加參數來查看錶單。 – Bradley

1

PARAMS是字符串。嘗試

@certificate.subscription_id = params[:subscription_id].to_i 
+0

嗯,甚至沒有警察,參數是一個字符串。好吧,請添加to_i方法,並以subscription_id提供。現在唯一的錯誤是subscription_id的輸入爲0.我查看過服務器,當GET方法從參數中獲取參數時,它顯示的參數是正確的,但是當插入到表中時它顯示爲0. – Bradley

+0

params [:subscription_id] =「1」? – margo

+0

不,我的意思是,如果我強制在我的控制器at.certificate.subscription_id = 1我可以得到subscription_id喂數據庫。 如果我把at.certificate.subscription_id = params [:subscription_id] 插入數據庫時​​它不會被識別。 所以我知道代碼工作,直到我從網址拉PARAMS。 http:// localhost:3000/certificates/new?subscription_id = 3 – Bradley

0

嘗試改變這個函數如下

def certificate_params 
    params.require(:certificate).permit(:document, :title, :subscription_id, :subscription) 
    end 

也許允許PARAMS許可證認購場。

+0

允許訂閱不起作用。不知道爲什麼它沒有拉入參數值。這是日誌。 – Bradley

+0

好像你錯過了你的subscription_id通入創建行動。使用隱藏字段。 –

相關問題