保存mongoid模型

时间:2012-07-18 06:02:06

标签: ruby-on-rails mongodb mongoid

我使用了下面的mongoid模型:

class Exercise
  include Mongoid::Document
  field :name, :type => String
  field :description, :type => String

  belongs_to :group

  validates_presence_of :name, :description
end

我想通过我的控制器坚持一个模型:

class ExercisesController < ApplicationController
  respond_to :json

  def create
    @exercise = Exercise.create(params[:exercise])
    @exercise.save!
    respond_with @exercise
  end
end

当我通过以下json:

"{\"_id\":\"50064f2e26a31829ca000001\",\"description\":\"Do something\",\"group_id\":\"4fbc6f5a26a3181742000004\",\"name\":\"Preacher curls\"}"

我收到此错误:

  

失败/错误:发布:创建,:运动=&gt; exercise.to_json        NoMethodError:          对于#

,未定义的方法“拒绝”

谁能看到我做错了什么?r

1 个答案:

答案 0 :(得分:0)

以此身份传递

params[:excercise] = JSON.parse("{\"_id\":\"50064f2e26a31829ca000001\",\"description\":\"Do something\",\"group_id\":\"4fbc6f5a26a3181742000004\",\"name\":\"Preacher curls\"}")