Rails 4和Paperclip,Unpermitted参数:attachment

时间:2016-03-03 12:55:41

标签: javascript ruby-on-rails ruby-on-rails-4 paperclip dropzone.js

我找到了一些解决方案,但都没有。

upload.rb

class Upload < ActiveRecord::Base
  belongs_to :issue
  # This method associates the attribute ":attachment" with a file attachment
  has_attached_file :attachment, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }

  # Validate the attached image is image/jpg, image/png, etc
  validates_attachment_content_type :attachment,
    :content_type => /\Aimage\/(jpg|jpeg|pjpeg|png|x-png|gif)\z/,
    :message => "file type is not allowed (only images)"
end

issue.rb

class Issue < ActiveRecord::Base
  has_many :uploads
  accepts_nested_attributes_for :uploads, allow_destroy: true
end

问题的部分形式:

<%= simple_form_for [@project, @issue], wrapper: :horizontal_form, html: { multipart: true, class: "form-horizontal", remote: true, id: "my-form" } do |f| %>
    <% f.input :title %>
        <div class="dropzone" id="dropzone-upload">
            <div class="fallback">
                <%= f.input :attachment, as: :file, multiple: true, name: "upload[attachment]" %>
            </div>
        </div>  
    <%= f.button :submit, id:"submit", class: "btn btn-default btn-primary" %>
<% end %>

issue_controller.rb

def issue_params
    params.require(:issue).permit(
      :title,
      uploads_attributes: [:id, :attachment, :issue_id]
    )
end

更新

确定我的控制台日志:

 Started POST "/projects/1/issues" for 127.0.0.1 at 2016-03-03 13:36:19 +0100
    Processing by IssuesController#create as HTML
      Parameters: {"utf8"=>"✓", "issue"=>{"title"=>"kljjl;kklj",  "attachment"=>"upload[attachment]"}
      Project Load (0.7ms)  SELECT  `projects`.* FROM `projects` WHERE `projects`.`id` = 1 LIMIT 1
    Unpermitted parameter: attachment
       (9.5ms)  COMMIT
    Redirected to http://localhost:3000/projects/1
    Completed 302 Found in 46ms (ActiveRecord: 4.5ms)

upload_controller.rb

def upload_params
    params.require(:upload).permit(:attachment)
end

这里的想法是: Paperlip有对象:附件。此对象在上载模型和模型与问题模型有关系。问题模型具有上传属性的表单。

更新16:45 03.03.16

我已更新表单以使用正确的嵌套属性但仍有相同的错误

<div class="dropzone" id="dropzone-upload">
    <div class="fallback">
      <%= f.simple_fields_for :uploads do |u| %>
        <%= u.input :attachment, as: :file, multiple: true %>
      <% end %>
    </div>
  </div>  

更新16:37

控制台日志:

Started POST "/projects/1/issues" for 127.0.0.1 at 2016-03-03 16:26:03 +0100
Processing by IssuesController#create as HTML
  Parameters: {"utf8"=>"✓", "issue"=>{"title"=>"wfwrv"}

2 个答案:

答案 0 :(得分:0)

这是因为如果您查看表单,文件输入正在使用&#34;附件&#34;因为它的参数名称为f.input :attachment

issue_params方法中,您不允许参数attachment,只允许嵌套参数上传[附件]。

调试此类问题时,您需要查看您的参数是什么。正如您在日志中看到的,它们看起来像这样:

Parameters: {"utf8"=>"✓", "issue"=>{"title"=>"kljjl;kklj",  "attachment"=>"upload[attachment]"}

因此,您提交问题[附件]。如果你喜欢它在你的参数中,那么只需删除&#34; uploads_attributes:&#34;从你的params方法。

应该看起来像:

def issue_params
    params.require(:issue).permit(
      :title,
      :id, 
      :attachment, 
      :issue_id
    )
end

您现在不应再看到警告了。顺便说一句,这只是一个警告。

如果您从不路由到上传,则不需要上传控制器。相反,要访问上传模型,请从问题控制器执行此操作,如下所示:

  1. 使用image = issue_params[:attachment]
  2. 从问题参数中获取附件
  3. upload = Upload.new(image)
  4. upload.save
  5. 如果您需要更多说明,请发表评论。

    修改

    看完你的模特后,不要Upload.new。相反,在#create方法中,使用新创建的问题创建连接上传:issue.uploads.new(image)。这一切都取决于你的#create方法是什么样的。我目前假设你在其中发了一个新问题,然后保存了。

    我还建议摆脱上传模式。 Paperclip是一个伟大的宝石,可以轻松上传所有麻烦。为什么要创建上传模型,Paperclip会无缝地将附件添加到您的问题模型中?

    编辑2:

    要制作嵌套表单,请按以下方式编写:

    <%= f.fields_for :uploads do |upload| %>
      <%= upload.input :attachment %>
    <% end %>
    

    并使用它代替您目前拥有的单个输入。那有意义吗?您实际上是在表单中嵌套表单。

答案 1 :(得分:0)

你试试这个吗?

<div class="dropzone" id="dropzone-upload">
<div class="fallback">
  <%= f.fields_for Upload.new do |u| %>
    <%= u.input :attachment, as: :file, multiple: true %>
  <% end %>
</div>