vendredi 2 janvier 2015

Rails Validates don't work on update


Vote count:

0




I've got a problem with validators. I have a "contact" model which contains two fields firstname and lastname and I want both required on CREATE and UPDATE method. When I create a record with no data, the server return me a 422 and do the rollback. This is ok. But when I update a record the server don't return the error 422 although the server does the rollback. And I need the return of the error to manage it on the client side.


So I use validators like this :



class Contact < ActiveRecord::Base
validates :lastname, presence: true
validates :firstname, presence: true
end


and my controller is:



class ContactsController < ApplicationController

respond_to :json

def index
respond_with Contact.all
end

def create
respond_with Contact.create(contact_params)
end

def show
respond_with Contact.find(params[:id])
end

def edit
respond_with Contact.find(params[:id])
end

def update
respond_with Contact.find(params[:id]).update(contact_params)
end

def destroy
respond_with Contact.find(params[:id]).destroy
end

private
def contact_params
params.require(:contact).permit(:lastname, :firstname, :position)
end

end


I have a serializer:



class ContactSerializer < ActiveModel::Serializer
attributes :id, :lastname, :firstname, :created_at, :updated_at
end


Someone could help me, please ?


Thanks by advance.



asked 3 mins ago







Rails Validates don't work on update

Aucun commentaire:

Enregistrer un commentaire