addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1linklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Handling Rails validations with Spine Models.

From: Stephen S.
Sent on: Thursday, September 13, 2012 6:20 PM
Greetings.

After a more-than-cursory search online - I'm swallowing my pride and come to you, hat in hand with a question.

Is there an established pattern for handing sever-side Validation errors in spine so the errors are reflected in your spine model? I'm not talking about spine's validate model that subclasses of Spine.Model have - more like errors like validates_presence_of.

The spinejs site suggests:

def create
  @page = Page.new(params[:page])
  if @page.save
    respond_with(@page, status: :created, location: @page)
  else
    respond_with(@page.errors, status: :unprocessable_entity) 
  end
end


Example

The ActiveRecord model Book validates_presence_of :name.
If your Rails app's BooksController#create action contains.

if @book.save
  respond_with @book
else
  respond_with(@book, status: :unprocessable_entity)
end

and you pop into a JS Console and run:

my_book = App.Book.create({}) //no name, will vail validation

my_book doesn't have the errors serialized on it - and that makes
sense because @book.to_json doesn't serialize @book's errors collection.

So - what's up - is there an established pattern for this?

If you help me I will get you a few beers...out of Pivotal's fridge.
You're welcome - no big.
-Stephen

Our Sponsors

  • Pivotal Labs

    Provides the awesome event space we use every month!

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy