ruby on rails - How to send params from nested forms? -


i'm making post request nested form written in reactjs such making ajax request create method of products controller.

react code:

i have empty object in getinitialstate this

getinitialstate: function() {  return {    products: [{name: '', price: '', quantity: ''}],    count: 1  }; }, 

when submit form,

handlesubmit: function(e) {   e.preventdefault();   var productsarray = this.state.products;   $.ajax({     data: {       product: productsarray     },     url: '',     type: "post",     datatype: "json",     success: function ( data ) {       console.log(data);       // this.setstate({ comments: data });     }.bind(this)   }); }, 

the object gets populated , parameter hash becomes

parameters: {"product"=>{"0"=>{"name"=>"", "price"=>"", "quantity"=>""}}, "shop_id"=>"gulshop"} 

so i'm getting

activerecord::unknownattributeerror (unknown attribute '0' product.): 

how can parameter hash this:

parameters: {"product"=>[{"name"=>"", "price"=>"", "quantity"=>""}], "shop_id"=>"gulshop"} 

what can done ?

your original error 'unknown attribute '0' product.' because product class not have attribute '0'. i'm not sure '0' coming haven't posted react code makes request.

you can make request component using jquery's .ajax method. e.g

$.ajax({   type: 'post',   url: '/your_url',   data: {     course: {       name: 'hello world',       price: 120     }   } }); 

you have following in controller..

class productcontroller < applicationcontroller   def create     @product = product.create(product_params)   end    private    def product_params     params.require(:product).permit(:name, :price)   end end 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -