title: Rails 3 - Showing forms with errors inside modalbox
body: <p>I am using modalbox for a modal window to submit forms that I learned from this <a href="http://net.tutsplus.com/tutorials/ruby/how-to-build-an-unobtrusive-login-system-in-rails/" rel="nofollow">Nettuts tutorial</a>. Right now it works when creating a new client in my model just fine, even when there is an error in the form - it allows me to stay in the modalbox until the error is corrected. I would like to also use it to edit a client. Almost everything works to do that, except when the edit client form throws an error. Instead of staying inside the modalbox window like when adding a new client, it refreshes to another page to complete the form. Here is what I have for the new client feature that is working:</p>
  
  <p>In my view:</p>
  
  <pre><code>&lt;%= link_to 'New Client', new_client_path, :id =&gt; 'newclient-link' %&gt;
  </code></pre>
  
  <p>In my ClientsController: </p>
  
  <pre><code>def create
  @client = Client.new(params[:client])
  
  respond_to do |format|
  if @client.save
      format.js { render :redirect} #modal form redirection (redirect.js.erb)
      format.html { redirect_to(@clients, :notice =&gt; 'Client was successfully created.') }
      format.xml  { render :xml =&gt; @client, :status =&gt; :created, :location =&gt; @client }
    else
      format.html { render :new }
      format.js
      format.xml  { render :xml =&gt; @client.errors, :status =&gt; :unprocessable_entity }
    end
  end
  end  
  </code></pre>
  
  <p>This is in the application.js file:</p>
  
  <pre><code>document.observe('dom:loaded', function() {
  $('newclient-link').observe('click', function(event) {
      event.stop();
      Modalbox.show(this.href,
          {title: 'Add New Client',
          width: 500,
          afterLoad: function() {
              $('new_client').observe('submit', function(event) {
                  event.stop();
                  this.request();
              })
          }}
      );
  });
  </code></pre>
  
  <p>})</p>
  
  <p>And finally, this is in the create.js.erb that is supposed to keep the form inside the modalbox:</p>
  
  <pre><code>$('MB_content').update("&lt;%= escape_javascript(render :partial =&gt; 'form') %&gt;");
  Modalbox.resizeToContent();
  $('new_clients').observe('submit', function(event) {
  event.stop();
  this.request();
  });
  </code></pre>
  
  <p>Now, having pasted all this, to replicate for the edit method, I duplicated the jquery in the application.js file and replaced "newclient-link" with "editclient-link" to match the link_to id in my edit link, and pretty much replicated the code for the create method in the controller and put it in the update method... However, I think this is where the problem lies. It seems like the solution should be simple and in the controller where ajax is supposed to be called to allow me to update the form in the modalbox even after throwing an error as it does with the new client link.</p>
  
  <p>Any thoughts?</p>