param没有将数据推送到Rails中的数据库的问题

时间:2017-05-22 20:02:51

标签: ruby-on-rails-4 simple-form strong-parameters

快一点。我正在尝试在创建对象时将参数推送到数据库中。如果我像下面的注释代码一样强制整数,我有它的工作但是无论出于何种原因,我无法将它添加到数据库中,我使用params [:subscription_id]方法。我可以看到我的服务器中的参数,但无法看到它插入到证书表中。如果我强制控制器中的整数,它将通过,我可以看到subscriptions_id传入。

这有可能与我的关系是一个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>

这可能是我丢失的愚蠢但我可以让它工作,如果我强制控制器中注释掉的代码中的整数。我几乎可以肯定params代码是正确的。感谢。

添加了日志

    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)

3 个答案:

答案 0 :(得分:1)

Params是字符串。尝试

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

答案 1 :(得分:1)

您需要在隐藏字段中设置subscription_id,以便在params散列中传递它。

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

这样就可以与其他参数一起设置。

答案 2 :(得分:0)

尝试更改此功能,如下所示

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

也许允许来自params的许可订阅字段。