Quick one. I'm trying to push a parameter into the database when i'm creating an object. I have it working if I force the integer as with the commented code below however for whatever reason I cannot get it to be added to the database is I use the params[:subscription_id] method. I can see the parameters in my server but cannot see it getting inserted into the certificates table. If i force the integer in my controller it will go through and I can see the subscriptions_id getting passed in.
Does this have any reason to do with my relationship being a has_one relationship perhaps?
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
View code:
<%= link_to "upload certificate", new_certificate_path(subscription_id: subscription.id) %>
Models
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
Certificate Form
<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>
This is probably something stupid i'm missing but I can get it to work if I force the integer in the commented out code in the controller. I'm nearly sure the params code is correct. Thanks.
Added logs
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)
You need to set your subscription_id
in a hidden field so that it gets passed in the params hash.
<%= f.hidden_field :subscription_id, :value => params[:subscription_id] %>
That way it gets set with the rest of the params.