Rails 3 + jQuery ; How to show error messages

Hi guys,

can anyone explain how to show error_messages_on (like back in Rails
2 , without Ajax) fields that didnt' pass the validation the jquery
way.

I googled for about 2 hours now and found nothing. Jquery works fine
and adds the content to my table, but im totally stuck with the whole
error/validation thing.

My form looks like this:

<%= form_for Translation.new , :remote => true do |f| %>
  <table>
  <tr>
    <td>
        <%= f.label :locale %><br />
        <%= f.text_field :locale %>

      </td>
    <td>
        <%= f.label :key %><br />
        <%= f.text_field :key %>
      </td>
    </tr>
  <tr>
    <td>
        <%= f.label :value %><br />
        <%= f.text_area :value , :rows => 3%>
      </td>
      <td>
        <%= f.label :interpolations %><br />
        <%= f.text_area :interpolations, :rows => 3 %>
      </td>
    </tr>
  <tr>
    <td>
        <%= f.label :is_proc %><br />
        <%= f.check_box :is_proc %>
      </td>
    <td></td>
  </tr>
  </table>
    <p><%= f.submit t('translation.create') %></p>
<% end %>

Create action like this:
   def create
    if @translation.save
      flash[:notice] = "Successfully created translation."
      respond_to do |format|
        format.html { redirect_to @translation }
        format.js
      end
    else
      respond_to do |format|
        format.html { render :action => 'new'}
        # show what went wrong with jquery
      end
    end
  end

and create.js.erb
/* Insert a notice between the last comment and the comment form */
$("#translation-notice").html('<div class="flash notice"><%=
escape_javascript(flash.delete(:notice)) %></div>');

/* Add the new comment to the bottom of the comments list */
$("#translations").prepend("<%=
escape_javascript(render(@translation)) %>");

/* Highlight the new comment */
$("#translation-<%= @translation.id %>").effect("highlight", {},
3000);

/* Reset the comment form */
$("#new_translation")[0].reset();

Adding validated data works but please give me hint with error
validation thing.

thanks

the problem is

def create

if @translation.save

  flash[:notice] = "Successfully created translation."

  respond_to do |format|

    format.html { redirect_to @translation }

    format.js

  end

else

  respond_to do |format|

    format.html { render :action => 'new'}

    # show what went wrong with jquery

  end

end

you can pass an action to format.js, apparently since most examples dont pass anything you didnt notice that format,js is doing this

format.js { render :action => “create”} and calls a create.js.erb

it does this if you only put format.js but you can render other actions like this

def create

if @translation.save

  flash[:notice] = "Successfully created translation."

  respond_to do |format|

    format.html { redirect_to @translation }

    format.js   { render :action => "success"}

  end

else

  respond_to do |format|

    format.html { render :action => 'new'}

    format.js   { render :action => "failure"}

  end

end

and have an success.js.erb with code for displaying the “everything went cool” updates and a failure.js.erb to display the “epic fail” updates.

there is no need to add a success or failure actions to the controller, when rails does not find and action in a controller it tries to display a template with the corresponding name anyway.

Thanks mate.

so, rename create.js.erb to success.js.erb and create a failure.js.erb and refer to format.js in the controller?

sure , add the corresponding logic to the failure.js.erb

refer to them in the create action like i showed , no need to create action in the controller just the template in the views folder