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 7608265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:19+00:00 2026-05-31T00:52:19+00:00

I am using Rails 3.2, I have a form and I want it to

  • 0

I am using Rails 3.2, I have a form and I want it to be posted via ajax and have the controller return json.

I am using a form_for helper like so:

= form_for(@object, :remote => true, :format => :json) do |f|
....

My objects controller create method looks like this:

  def create
    respond_to do |format|
      if @object.save
         format.html { redirect_to @object }
         format.json { render json: @object, status: :created, location: @object }
      else
        format.html { render action: "new" }
        format.json { render json: @object.errors, status: :unprocessable_entity }
      end
    end
  end

The form is submitting ajaxly as expected. But the controller is returning html, not json!

Inspecting the request with firebug and sure enough the Content-Type http header on the ajax request is being set to application/html.

The documentation around this is pretty sparse, :format => :json seems to just append “.json” to the forms action, not actually modify any http headers.

I’ve also tried :content_type => :json to no effect.

I can’t simply hard code the controller to return json as there are other places where I do want it to return html…

So does anyone know how to tell the controller to render json when using form_for?

Thanks for any help

  • 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-31T00:52:21+00:00Added an answer on May 31, 2026 at 12:52 am

    You can set the content-type with:

    = form_for(@object, :remote => true, :html => {:'data-type' => 'json'})

    As described in rails.js line 106.

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

Sidebar

Related Questions

I would like to have Ajax form in Rails so i'm using form_remote_tag. The
I'm using Rails 3.1.0.rc5. I want to have a form with a pair of
I have a form I am submitting via AJAX (using prototype and the built-in
I am using rails 2.3 and have a problem. I have remote form, and
I'm a RoR beginner and am using Rails 3.2.3. I have a search form
I'm using the Simpleform gem for my rails app and I have a form
Using Ruby 1.8.7-p358, Rails 3.0.12, gem responder, gem simple-form; jquery_ujs.js I have a form
I have started using jQuery and rails. I have made a simple form that
I am using Rails 3.1 and want to have a Sass variable (or other
I have a AJAX form in Rails 3. Given the nature of my app,

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.