【问题标题】:Rails Paperclip Gem File Not UploadingRails Paperclip Gem 文件未上传
【发布时间】:2014-07-17 02:33:29
【问题描述】:

我安装了 Paperclip gem,但文件没有正确上传。我正在尝试将文件上传到我的合同模型中。由于对图像文件的存在进行了验证,因此我可以判断该文件没有上传,因为我收到“图像不能为空白错误”。此外,如果我取消该验证,该文件不会显示在我的 show.html.erb 视图中。

这是我的合同模型:

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

这是我的合同表格:

<%= 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 %>

最后是我的合同/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 %>

谢谢!!

【问题讨论】:

  • 尝试改变表单 {:multipart => true } do |f| %>
  • 那没用...它给了我一个可怕的错误,而不是@ramamoorthy_villi
  • 你能发布你的ContractController吗?
  • has_attached_file :photo validates_presence_of :photo @JKen13579
  • 不,我的意思是 contracts 控制器,特别是 contract_params 方法。

标签: ruby-on-rails upload paperclip


【解决方案1】:

您可以在控制器中检查您的强参数声明,并将 :image 附加到白名单属性之一。

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

这允许将 Paperclip 迁移的新 :image 属性列入白名单。

【讨论】:

    【解决方案2】:

    正如我们在 cmets 中建立的,您忘记将 :image 添加到您的 contract_params 方法中。经典strong_params 错误。这发生在我们所有人身上。

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 2013-09-04
      • 2012-08-18
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多