views:

30

answers:

1

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
+1  A: 

Well, you can do

format.js { render :update { |page| page.replace_html "inline_form", :partial => "form" }

where inline_form is the id of the element inside which your inline editor sits. The form partial can contain an error_messages_for statement and the actual field that you are showing.

That way, if validation fails, the element with your editor will be updated with the same editor, but displaying validation errors.

neutrino
ahh nice. Thats excellent. Thanks for that.
adam