title: Rails - Cant get errors to display on a failed ajax form submit
body: <p>I've been trying to get this for a while now but cant get any errors to display when a user submits a form via ajax and the form fails (I cant get a success message to display either but thats not as big of a deal). </p>
  
  <p>Here is what I have</p>
  
  <p>The form</p>
  
  <pre><code>&lt;ul id="errors"&gt;
  
  &lt;/ul&gt;
  
  &lt;%= form_for([@guide, @category, @key], url: guide_categories_keycreate_path(category_id: params[:id]), remote: true, :authenticity_token =&gt; true) do |f| %&gt;
     &lt;%= render 'shared/error_messages', object: f.object %&gt;
  
     &lt;%= f.label :name, "Key name" %&gt;
     &lt;%= f.text_field :name %&gt;
  
      &lt;%= f.select :key_type, [['Value', 1], ['Text', 2], ['Image', 3]] %&gt;
  
     &lt;%= f.submit "Next", :value =&gt; "Add New Key"  %&gt;
  &lt;% end %&gt; 
  </code></pre>
  
  <p>The js render line in the controller on if key doesn't save</p>
  
  <pre><code>...
  else
      respond_to do |format|
       format.html {  redirect_to edit_guide_category_path(@guide, @category) }
            format.json { render :json =&gt; { :error =&gt; @key.errors.full_messages }, :status =&gt; 422 }
       format.js 
    end
  </code></pre>
  
  <p>javascript</p>
  
  <pre><code>$(document).on "ajax:error", "form", (evt, xhr, status, error) -&gt;
     errors = xhr.responseJSON.error
     for message of errors
        $('#errors ul').append '&lt;li&gt;' + errors[message] + '&lt;/li&gt;'
  </code></pre>
  
  <p>The form isn't in its normal key/create view, incase something in the code confuses you that might explain why it's that way.</p>
  
  <p>Dont know where to go from here, I basically copied the coding from <a href="http://tomdallimore.com/blog/ajax-and-json-error-handling-on-rails/" rel="nofollow">this</a> tutorial. I have looked at and tried a few tutorials and seen a few stackoverflow questions and this is basically all I need but wont work. Not sure what to do next. </p>