Rails and jQuery - how do you get server-side validation errors to your view after an ajax request

Posted by adam on Stack Overflow See other posts from Stack Overflow or by adam
Published on 2010-03-16T10:59:01Z Indexed on 2010/03/16 11:46 UTC
Read the original article Hit count: 255

Filed under:
|

Ive searched this site but questions are usually regarding doing client-side validations or for different frameworks.

I have a tasks list whose items can be edited inline. Upon submitting the inline edit form the item is updated all thanks to jQuery, ajax and rails.

But I want to handle bad input from the user. HTML requests redisplay the view and errors are displayed thanks to rails helpers.

But how do I insert that information after an ajax call?

Heres my update method in my controller

def update
    @task = Task.find(params[:id])

    respond_to do |format|
      if @task.update_attributes(params[:task])
        flash[:notice] = 'Task was successfully updated.'
        format.html { redirect_to(@task) }
        format.xml  { head :ok }
        format.js
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
        #format.js ...hmmm... either go to js.erb file or do stuff inline
      end
    end
  end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about jQuery