Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 936339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:18:01+00:00 2026-05-15T21:18:01+00:00

I am calling a controller’s create action via ajax so when the object saves

  • 0

I am calling a controller’s create action via ajax so when the object saves successfully, the js response is triggered. However, if the object fails to save due to validation, then I want the response to be html. I hope to achieve this by not returning the js response in the else block (please see code below) but this produces a 406 Not Acceptable error.

Any ideas on how to do this?

I should also probably note that the reason why I want to do this is because I am unsure how to create an appropriate js response if the validation fails…

Thanks in advance!!! =]

Controller create action

respond_to do |format|
  if @person.save
    flash[:notice] = 'Successfully created.'
    format.html { redirect_to(@person) }
    format.js
  else
    flash[:error] = 'There are errors while trying to create a new Person'
    format.html { render :action => "new" }
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:18:02+00:00Added an answer on May 15, 2026 at 9:18 pm

    If you are specifically requesting the js format in the URL, then you have to provide JS for your response. One option instead would be to not specify a format in the request, and then just filter your responses for xhr. Like so:

    respond_to do |format|
      if @person.save
        flash[:notice] = 'Successfully created.'
        if request.xhr?
          format.js
        end
        format.html { redirect_to(@person) }
      else
        flash[:error] = 'There are errors while trying to create a new Person'
        format.html { render :action => "new" }
      end
    end
    

    This way, if you make a request without specifying ANY format, it will hit js first, IF it’s an xhr request (ie, an AJAX request), whereas, if there is an error, it will return html, no matter what.

    Does that make sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Log model, that records the fact of calling controller's action.
I'm encounting a method now allowed exception when calling a controller action which accepts
When calling php via cli, the current directory is NOT changed to the one
I have referred http://bakery.cakephp.org/articles/view/calling-controller-actions-from-cron-and-the-command-line and created cron_dispatcher.php and placed it in the app folder.
I have used response.redirect in classic ASP and ASP.NET webforms. However, with MVC 2.0,
I am calling a controller method SendMe(formCollection myForm){}. But I don't know how to
I have controller method in my MVC application that I am calling using the
I have a View Controller calling a method in its own view called closeMenu.
In a Spring 3 app a controller is calling a JpaCollectorManager with calls a
When I try to use Html.Action in ASP.NET MVC 3 for a controller in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.