title: How to pass parameters when using JSON?
body: <p>I can't get my images to save because of an <code>Unpermitted parameter: image
   error</code>. Using <a href="http://www.dropzonejs.com/" rel="nofollow">dropzone.js</a>, I have to upload the form by JSON if I want to have to ability to drag and drop images on the file field. I'm trying to allow only one image to be uploaded to the model. Here is my code:</p>
  
  <blockquote>
    <p>items.rb</p>
  </blockquote>
  
  <pre><code>class Item &lt; ActiveRecord::Base
  .....
  validates_inclusion_of :found, in: [true, false]
  
  has_attached_file :image, :styles =&gt; { :medium =&gt; "350x350&gt;", :thumb =&gt; "100x100&gt;" }
  validates_attachment :image,
          content_type: { content_type: [ "image/jpg", "image/jpeg", "image/png" ] },
          size: { in: 0..1.megabytes },
          matches: [/png\Z/, /jpe?g\Z/]
  
  end
  </code></pre>
  
  <blockquote>
    <p>items.coffee </p>
  </blockquote>
  
  <pre><code>  $('#new_item').dropzone
      acceptedFiles: '.jpeg, .jpg, .png'
      maxFilesize: 1
      maxFiles: 100
      addRemoveLinks: true
      paramName: 'item[image]'
      clickable: '#image-preview'
      headers: "X-CSRF-Token" : $('meta[name="csrf-token"]').attr('content')
      previewsContainer: '#image-preview'
      thumbnailWidth: 300
      thumbnailHeight: 300
      autoProcessQueue: false
      uploadMultiple: true
      parallelUploads: 100
      init: -&gt;
        myDropzone = this
        @element.querySelector('#item-submit').addEventListener 'click', (e) -&gt;
          e.preventDefault()
          e.stopPropagation()
          if myDropzone.getQueuedFiles().length &gt; 0
            myDropzone.processQueue()
          else
            $("#new_item").submit();
          return
        @on 'sendingmultiple', -&gt;
          return
        @on 'successmultiple', (files, response) -&gt;
          return
        @on 'errormultiple', (files, response) -&gt;
          return
        return
  </code></pre>
  
  <blockquote>
    <p>items_controller.rb</p>
  </blockquote>
  
  <pre><code>class ItemsController &lt; ApplicationController
  
        def create
          @item = current_user.items.build(item_params)
          if @item.save
            if request.xhr?
              render json: {
                location: url_for(controller: 'items', action: 'continue', id: @item),
                flash: {notice: "Successfully posted item!"}
              }
            else
              redirect_to(controller: 'items', action: 'continue', id: @item)
            end
          else
            render json: @item.errors, status: :unprocessable_entity
          end
        end
  
    private
  
    def item_params
      params.require(:item).permit(:name, :description, :image)
    end
  
    def find_item
      @item = Item.find(params[:id])
    end
  
  end
  </code></pre>
  
  <blockquote>
    <p>routes.rb</p>
  </blockquote>
  
  <pre><code>  resources :items do
      collection do
        get 'lost', to: 'items#index_lost'
        get 'found', to: 'items#index_found'
      end
      get '/post/continue', to: 'items#continue', on: :member
    end
  </code></pre>
  
  <blockquote>
    <p>items/_form.haml</p>
  </blockquote>
  
  <pre><code>= form_for @item, validate: true, html: {multipart: true} do |f|
      = f.label :name, class: 'col-lg-2 control-label'
          = f.text_field :name
      %main#image-preview
          .fallback
              = f.file_field :image, multiple: false
      .submit-section
          .col-lg-12.col-md-12
              = f.submit 'Done', id: 'item-submit'
  </code></pre>
  
  <p>(Note: <code>validate: true</code> is from the <code>client_side_validation gem</code>)</p>
  
  <p>So the error I get after submitting the form is: </p>
  
  <pre><code>Started POST "/items" for 127.0.0.1 at ............
  Processing by ItemsController#create as JSON
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"SAIGiG9SjnuTKfsmxszBFfK+lZVojS2UmDi9hTeC8wI6B2fGZ+sZM9te3lIFkFsT30SdFWjyXZMZI7zXAlAMGA==", "item"=&gt;{name"=&gt;"Post Item", image"=&gt;{"0"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x007f3d248b5a98 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20151002-2427-1cvzgsi.jpg&gt;, @original_filename="haribo.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[image][0]\"; filename=\"haribo.jpg\"\r\nContent-Type: image/jpeg\r\n"&gt;}}, "null"=&gt;"", "commit"=&gt;"Done"}
  Unpermitted parameter: image
  ..........
      Completed 200 OK in 230ms (Views: 0.3ms | Searchkick: 31.1ms | ActiveRecord: 14.3ms)
  </code></pre>
  
  <p>Now after all of this it still saves but it just causes image not to because I also want to allow the user to submit the form without an image. How do I fix this?</p>