简体   繁体   中英

Rails Paperclip Gem File Not Uploading

I installed the Paperclip gem but the file is not uploading correctly. I am trying to upload a file into my Contract model. Since put in a validation for presence of an image file, I can tell that the file did not upload because I get the "Image can't be blank error". Furthermore, if I take out that validation, the file doesn't show up in my show.html.erb view.

Here is my Contract model:

class Contract < ActiveRecord::Base

    has_attached_file :image
    validates_presence_of :image
    has_many :task_orders, :dependent => :destroy
    validates_uniqueness_of :id
    validates_presence_of :id
    self.primary_key = :id
    validates :awardAmount, :numericality => true

end

Here is my Contract form:

<%= form_for(@contract, :html => {:multipart => true}) do |f| %>
  <% if @contract.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@contract.errors.count, "error") %> prohibited this contract     from being saved:</h2>

  <ul>
  <% @contract.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
   <% end %>
     </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label "Contract Number" %><br>
    <%= f.text_field :id %>
  </div>
  <div class="field">
    <%= f.label 'Contract Name'%><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label "Award Date" %><br>
    <%= f.date_select :awardDate %>
  </div>
  <div class="field">
    <%= f.label "Expiration Date"%><br>
    <%= f.date_select :expirationDate %>
  </div>
  <div class="field">
    <%= f.label "Award Amount"%><br>
    <%= f.text_field :awardAmount %>
  </div>
  <div class="field">
    <%= f.label "Image"%><br>
    <%= f.file_field :image %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And lastly here is my contract/show.html.erb

<p id="notice"><%= notice %></p>
<p>
  <strong>Contract ID:</strong>
  <%= @contract.id %>
</p>
<p>
  <strong>Contract Name:</strong>
  <%= @contract.name %>
</p>
<p>
  <strong>Award Date:</strong>
  <%= @contract.awardDate %>
</p>
<p>
  <strong>Expiration Date:</strong>
  <%= @contract.expirationDate %>
</p>
<p>
  <strong>Award Amount:</strong>
  <%= number_to_currency(@contract.awardAmount) %>
</p>
<p>
  <strong>Obligated Amount:</strong>
  <%= number_to_currency(@contract.obligatedAmount) %>
</p>
<p>
  <strong>Invoiced to Date:</strong>
  <%= number_to_currency(@contract.invoicedAmount) %>
</p>
<% if @contract.image? %>
<p>
  <strong>Attachment:</strong>
  <%= link_to @contract.image.url, @contract.image.url %>
</p>
<% end %>

Thank you!!

You can check your strong params declaration in your controller and append :image to one of the whitelisted attributes.

def contract_params
  params.require(:section).permit(:id, :name,:awardDate, :expirationDate, :awardAmount,  :image)
end

This allows for whitelisting to your the new :image attribute migrated by Paperclip.

As we established in the comments, you forgot to add :image to your contract_params method. Classic strong_params mistake. It happens to us all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM