Ruby on rails Rails 5 Mongoid模型未定义方法\u id有\u多个关系

Ruby on rails Rails 5 Mongoid模型未定义方法\u id有\u多个关系,ruby-on-rails,ruby,mongodb,mongoid,Ruby On Rails,Ruby,Mongodb,Mongoid,我正在编写一个配方rails api,数据库是Mongodb。我很难创建一个JSON帖子来模拟配方,例如: { "recipe": { "name" : "Chicken", "serving" : "3", "macro" : { "protein": "3", "carb": "10", "fat" : "5" }, "user_id

我正在编写一个配方rails api,数据库是Mongodb。我很难创建一个JSON帖子来模拟配方,例如:

{
"recipe":   {
        "name" : "Chicken",
        "serving" : "3",
        "macro" : {
            "protein": "3",
             "carb": "10",
             "fat" : "5"
        },
        "user_id" : "587d5dccb3e9664e280a1199", 
        "ingredients" : [{
            "name" : "Chicken Breast",
            "value" : "3.123"
        }]
    }
}
成分是一种有很多关系的关系,我正在使用accepts\u嵌套的属性\u来嵌入。它需要的是在它被创造出来之前的成分id。它在发布时给了我以下错误:

"#<NoMethodError: undefined method `_id' for {\"name\"=>\"Chicken Breast\", \"value\"=>\"3.123\"}:ActiveSupport::HashWithIndifferentAccess>"
宏观模型:

class Macro
  include Mongoid::Document
  field :protein, type: Integer
  field :carb, type: Integer
  field :fat, type: Integer

  embedded_in :recipe

  validates :protein, :carb, :fat, presence: true
end
成分模型:

class Ingredient
  include Mongoid::Document

  field :name, type: String
  field :value, type: String

  belongs_to :recipe, inverse_of: :ingredients

  validates :name, :value, presence: true
end
这是我的食谱控制器:

class Api::V1::RecipeController < ApiController
  def index
    @recipes = Recipe.all
    render json: @recipes
  end

  def create
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      render json: @recipe
    else
      render :json => { :errors => @recipe.errors }, :status => 422
    end
  end

  def show
    @recipe = Recipe.find(params[:id])
    render json: @recipe
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.destroy
    render :nothing, status: :no_content
  end

  private

  def recipe_params
    params.require(:recipe).permit(:name, :serving, :user_id, macro: [:protein, :carb, :fat], ingredients: [:name, :value])
  end

end
类Api::V1::RecipeController{:errors=>@recipe.errors},:status=>422
结束
结束
def秀
@recipe=recipe.find(参数[:id])
呈现json:@recipe
结束
def销毁
@recipe=recipe.find(参数[:id])
@毁灭
呈现:无,状态::无内容
结束
私有的
def配方参数
参数要求(:配方)。允许(:名称,:服务,:用户id,宏:[:蛋白质,:碳水化合物,:脂肪],成分:[:名称,:值])
结束
结束

我不明白为什么它给我发布时的错误。谢谢你的帮助

放置一个
绑定。在
def create
之后的行上撬动
,并检查
#配方参数
的返回值。似乎无法使用传递的哈希值实例化
配方。请将
绑定。在
def create
后的行上撬动
,并检查
配方参数的返回值。似乎无法用传递的哈希值实例化
配方。
class Api::V1::RecipeController < ApiController
  def index
    @recipes = Recipe.all
    render json: @recipes
  end

  def create
    @recipe = Recipe.new(recipe_params)
    if @recipe.save
      render json: @recipe
    else
      render :json => { :errors => @recipe.errors }, :status => 422
    end
  end

  def show
    @recipe = Recipe.find(params[:id])
    render json: @recipe
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.destroy
    render :nothing, status: :no_content
  end

  private

  def recipe_params
    params.require(:recipe).permit(:name, :serving, :user_id, macro: [:protein, :carb, :fat], ingredients: [:name, :value])
  end

end