Handling Ajax Form Validation in Rails

Handling Ajax requests errors isn’t documented really well and there are many ways to accomplish this, some are simple and some are pretty darn complicated.

the easiest solution I found was rendering different partials depending on the status of the save or update methods, here is a snippet

  def update
    @bid = Bid.find(params[:id])

    respond_to do |format|
      if @bid.update_attributes(params[:bid])
        flash[:notice] = 'Bid was successfully updated.'
        format.js { render :partial => "update" }
      else
        format.js { render :partial => "update_fail"}
      end
    end
  end

It may not be the most elegant solution but is sure simple and does what it says it does.

Leave a comment