lundi 2 février 2015

Controller preventing Backbone ajax post in Rails


I am making a Backbone ajax post call in my Rails App using a formData. The formData is successfully passing to the controller but the controller is preventing params from passing. I am getting a "ActionController::ParameterMissing (param is missing or the value is empty: product)"



var formData = new FormData();
formData.append("product_name","ring");
formData.append("image",$('input#image')[0].files[0]);
$.ajax({
dataType: "json",
url: "/products",
data: formData,
processData: false,
contentType: false,
type: 'post'
}


I know it is because my params is missing the value "product". The return for the formData looks like



=> {"product_name"=>"milk",
"image"=>
#<ActionDispatch::Http::UploadedFile:0x000000047ec788
@content_type="image/png",
@headers=
"Content-Disposition: form-data; name=\"image\"; filename=\"playerSprite.png\"\r\nContent-Type: image/png\r\n",
@original_filename="playerSprite.png",
@tempfile=#<File:/tmp/RackMultipart20150130-3021-17wsn2y>>,
"action"=>"create",
"controller"=>"products"}


can someone tell me how to format my formData to have the value "product" in my params so it can pass the controller


My controller action for create is



def create
product = Product.create(product_params)
render json: product
end

def product_params
params.require(:product).permit(:product_name,:image)
end




Aucun commentaire:

Enregistrer un commentaire