Respond with JS on create-success, otherwise respond with HTML

Hey fellas,

Is there a way to selectively respond with a format depending on whether there were errors creating an object or not?

My goal is to have a remote form fall back to the html version of the ‘new’ action if there were errors. In other words, run the create.js.erb if the save succeeds, otherwise render the html ‘new’ action.

Say I have the following create method:

def create

@message =[:message])

respond_to do |format|


format.html {

redirect_to messages_path, notice: ‘Your message was successfully sent.’




render action: “new” # This does not work when I have a remote form.




This will work fine (On both remote html and js formats ) if the @message saves, but when I try and do this using Ajax and they were errors, the render call responds with html that gets ignored by the browser (who is expecting js). How can force the browser to render the HTML version of ‘new’ even if the original action was JS?

Any ideas on this?