简体   繁体   English

在Rails表单的file_field中使用Dropzone.js

[英]Using Dropzone.js in a file_field within a Rails form

I am trying to use Dropzone.js to upload files within my Rails app. 我正在尝试使用Dropzone.js在我的Rails应用程序中上传文件。 It seems that if I use the standard setup, the entire form becomes an image upload field. 看来,如果我使用标准设置,则整个表单将成为图像上载字段。 However, my form contains other fields as well. 但是,我的表单也包含其他字段。 I only want to use Dropzone.js in a file_field area. 我只想在file_field区域中使用Dropzone.js。

Steps I've used are: 我使用的步骤是:

Gemfile 的Gemfile

gem 'rails-assets-dropzonejs', source: 'https://rails-assets.org'

application.js 的application.js

//= require dropzonejs

application.css application.css

*= require dropzonejs

_form.html.erb _form.html.erb

<%= form_for @activity, html: {class: 'ui form'} do |f| %>
  <!-- Fields like this one don't need to be dropzone fields -->
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <!-- The following field does -->
  <div class="field">
    <%= f.label :gallery_images %>
    <%= f.file_field :gallery_images, multiple: true, class: 'drop' %>
    <%= f.hidden_field :gallery_images_cache %>
  </div>
<% end %>

activities.coffee activities.coffee

$ ->
  $('.drop').dropzone({ url: "/activities/post" });

As you can see, I'm trying to bind Dropzone to the 'drop' class which I've attached to the file_field. 如您所见,我正在尝试将Dropzone绑定到我附加到file_field的'drop'类。 However, this doesn't seem to work correctly and I am seeing no errors in the console. 但是,这似乎无法正常工作,并且我在控制台中看不到任何错误。

Anyone have an idea how I'd get Dropzone.js to work for a file_field within a Rails form? 有人知道我如何让Dropzone.js在Rails表单中的file_field中工作吗? Or can I only bind it to the entire form? 还是只能将其绑定到整个表单?

Any help is much appreciated! 任何帮助深表感谢! Thanks! 谢谢!

You need to permit the :file param. 您需要允许:file参数。 Most probably there will be code something along the line of 很可能会有一些类似的代码

private 

  def activities_params
      params.permit(:name, ...other_params)
  end

Add :file to the permit method :file添加到allow方法

private 私人的

  def activities_params
      params.permit(...other_params, :file)
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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