Reputation: 13721
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!!
Upvotes: 0
Views: 504
Reputation: 137
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.
Upvotes: 2
Reputation: 9443
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.
Upvotes: 1