Ruby on rails 我如何将数据发布到rails后端,并将其与多个关联起来?

Ruby on rails 我如何将数据发布到rails后端,并将其与多个关联起来?,ruby-on-rails,associations,ruby-on-rails-5,postman,Ruby On Rails,Associations,Ruby On Rails 5,Postman,如下 我创建了一个与两个表的关联 我在控制台添加的数据是: Deal.create(items: "items1", shop_id:1, customer_ids: "uuid") 这就是成功 但是当我试着用邮递员来测试 {"shop_id": 1, "items": "items2", "customer_ids": "uuid"} 新的数据客户是null 我的控制器: def create @deal = Deal.new(deal_params) respond_t

如下 我创建了一个与两个表的关联

我在控制台添加的数据是:

Deal.create(items: "items1", shop_id:1, customer_ids: "uuid")
这就是成功

但是当我试着用邮递员来测试

{"shop_id": 1, "items": "items2", "customer_ids": "uuid"}
新的数据客户是
null

我的控制器:

def create
    @deal = Deal.new(deal_params)

    respond_to do |format|
      if @deal.save
        format.json { render :show, status: :created }
      else
        format.json { render json: @deal.errors, status: :unprocessable_entity }
      end
    end
  end
def deal_params
    params.require(:deal).permit(:items, :shop_id, :customer_ids)
end
我的路线:

post 'deals', to: 'deals#create'
日志未出现错误

我的情况有什么问题

更新

我直接在控制器上尝试相同的数据

@deal = Deal.new({"shop_id": 1, "items": "items2", "customer_ids": "uuid"})
它成功了

所以我认为问题出在我的deal_params函数上

我试试教程

params.require(:deal).permit(:items, :shop_id, :customer_ids => [])
但还是不行

{"deal" => {"shop_id" => 1, "items" => "items2", "customer_ids" => "uuid"}}
在您的控制器中,私有操作需要
deal
,因此这里您需要将数据作为散列发送到
deal
的包装器中

params.require(:deal).permit(:items, :shop_id, :customer_ids)

由于您在允许其他人之前放置了
params.require(:deal)
,rails仅在
:deal
下获取参数。所以输入数据必须是

{"deal": {"shop_id": 1, "items": "items2", "customer_ids": "uuid"}}
或者您可以尝试直接允许其他值,如

params.permit(:items, :shop_id, :customer_ids => [])
试试这个:

params.require(:deal).permit(:items, :shop_id, customer_ids: [])

你是什么样的亲戚?有很多麻烦吗?有很多吗?有吗

路线是什么。你在什么地方调用了
create
操作了吗?@HaseebEqx我已经添加了可能是我的表达式错误了。在我的例子中,
{“shop\u id”:1,items:“items2”}
已经插入了数据库,但是只有
{customer\u id:“uuid”}
没有,所以我不明白为什么。另外,将content type设置为application/jsonHell耶,这是成功的,但是私有代码应该是
参数permit(:items,:shop\u id,:customer\u id=>[])