Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何从嵌套表单发送参数?_Ruby On Rails_Arrays_Ruby_Reactjs - Fatal编程技术网

Ruby on rails 如何从嵌套表单发送参数?

Ruby on rails 如何从嵌套表单发送参数?,ruby-on-rails,arrays,ruby,reactjs,Ruby On Rails,Arrays,Ruby,Reactjs,我从reactjs中编写的nestedform发出POST请求,这样它就发出了ajax请求,请求创建产品控制器的方法 反应代码: 我在getInitialState中有一个空对象,如下所示 getInitialState: function() { return { products: [{name: '', price: '', quantity: ''}], count: 1 }; }, 当我提交表格时 handleSubmit: function(e) { e.pre

我从
reactjs
中编写的
nestedform
发出
POST
请求,这样它就发出了
ajax
请求,请求
创建
产品控制器的
方法

反应代码:

我在
getInitialState
中有一个空对象,如下所示

getInitialState: function() {
 return {
   products: [{name: '', price: '', quantity: ''}],
   count: 1
 };
},
当我提交表格时

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)
  });
},
对象被填充,
参数
散列如下

Parameters: {"product"=>{"0"=>{"name"=>"", "price"=>"", "quantity"=>""}}, "shop_id"=>"gulshop"}
所以我要

ActiveRecord::UnknownAttributeError (unknown attribute '0' for Product.):
如何获得如下所示的
参数
散列:

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

可以对此执行什么操作?

您的原始错误“产品的未知属性“0”是因为产品类没有属性“0”。我不确定“0”来自何处,因为您尚未发布发出请求的react代码

您可以使用jQuery的.ajax方法轻松地从组件发出请求。e、 g

$.ajax({
  type: 'POST',
  url: '/your_url',
  data: {
    course: {
      name: 'Hello World',
      price: 120
    }
  }
});
然后,您的控制器中会有如下内容:

class ProductController < ApplicationController
  def create
    @product = Product.create(product_params)
  end

  private

  def product_params
    params.require(:product).permit(:name, :price)
  end
end
class-ProductController