0

モバイル アプリケーションのテスト中に、空の JSON を渡して生徒のレコードを作成しようとしました。

  Parameters: {"student"=>{}}
WARNING: Can't verify CSRF token authenticity
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "students" ("course_id", "created_at", "icon", "name", "password", "status", "studentID", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["course_id", nil], ["created_at", Mon, 08 Oct 2012 13:07:12 UTC +00:00], ["icon", nil], ["name", nil], ["password", nil], ["status", "pending"], ["studentID", nil], ["updated_at", Mon, 08 Oct 2012 13:07:12 UTC +00:00]]
   (3.5ms)  commit transaction
   (0.1ms)  begin transaction
  Student Exists (0.3ms)  SELECT 1 AS one FROM "students" WHERE ("students"."studentID" IS NULL AND "students"."id" != 46) LIMIT 1
   (0.1ms)  rollback transaction
Completed 422 Unprocessable Entity in 61ms (Views: 0.5ms | ActiveRecord: 5.2ms)

私のモデルは、特定のフィールドの存在を検証します。

validates :name, :password, :status, :studentID, :presence =>true
  validate :validate_course_id
  validates :studentID, :uniqueness=>{:message=>"This studentID already exists"}

created_atJSON は空ですが、 、updated_at、およびを除くほとんどすべてのフィールドが null のレコードが作成されますpending

コントローラーで:

  def create
    @student = Student.new(params[:student])
    # @student.update_attribute(:status,'pending')
    @student.status = 'pending'

    respond_to do |format|
      if @student.save
        upload_icon(params[:student][:icon_upload])

        format.html { redirect_to @student, notice: 'Student was successfully created.' }
        format.json { render json: @student, status: :created, location: @student }
      else
        format.html { render action: "new" }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
4

1 に答える 1

1

の場合update_attribute:

単一の属性を更新し、通常の検証手順を経ずにレコードを保存します。これは、既存のレコードのブール フラグに特に役立ちます。検証モジュールが混在している場合、Baseの通常のupdate_attributeメソッドはこれに置き換えられます。これはデフォルトです。

の場合update_attributes:

渡されたハッシュからすべての属性を更新し、レコードを保存します。オブジェクトが無効な場合、保存は失敗し、false が返されます。

于 2012-10-08T14:41:17.790 に答える