ruby on rails - How to pass parameters when using JSON? -


i can't images save because of unpermitted parameter: image error. using dropzone.js, have upload form json if want have ability drag , drop images on file field. i'm trying allow 1 image uploaded model. here code:

items.rb

class item < activerecord::base ..... validates_inclusion_of :found, in: [true, false]  has_attached_file :image, :styles => { :medium => "350x350>", :thumb => "100x100>" } 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 

items.coffee

  $('#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: ->       mydropzone =       @element.queryselector('#item-submit').addeventlistener 'click', (e) ->         e.preventdefault()         e.stoppropagation()         if mydropzone.getqueuedfiles().length > 0           mydropzone.processqueue()         else           $("#new_item").submit();         return       @on 'sendingmultiple', ->         return       @on 'successmultiple', (files, response) ->         return       @on 'errormultiple', (files, response) ->         return       return 

items_controller.rb

class itemscontroller < 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 

routes.rb

  resources :items     collection       'lost', to: 'items#index_lost'       'found', to: 'items#index_found'     end     '/post/continue', to: 'items#continue', on: :member   end 

items/_form.haml

= form_for @item, validate: true, html: {multipart: true} |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' 

(note: validate: true client_side_validation gem)

so error after submitting form is:

started post "/items" 127.0.0.1 @ ............ processing itemscontroller#create json   parameters: {"utf8"=>"✓", "authenticity_token"=>"saigig9sjnutkfsmxszbffk+lzvojs2umdi9htec8wi6b2fgz+szm9te3lifkfst30sdfwjyxzmzi7zxalamga==", "item"=>{name"=>"post item", image"=>{"0"=>#<actiondispatch::http::uploadedfile:0x007f3d248b5a98 @tempfile=#<tempfile:/tmp/rackmultipart20151002-2427-1cvzgsi.jpg>, @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">}}, "null"=>"", "commit"=>"done"} unpermitted parameter: image ..........     completed 200 ok in 230ms (views: 0.3ms | searchkick: 31.1ms | activerecord: 14.3ms) 

now after of still saves causes image not because want allow user submit form without image. how fix this?

you submitted params include image attribute hash

image"=>{"0"=>#<actiondispatch::http::uploadedfile:0x007f3d248b5a98 ... 

it should :

"image"=>#<actiondispatch::http::uploadedfile:0x007f3d248b5a98 @tempfile= ... 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -