Shital luitel
Shital luitel

Reputation: 135

Change html response to json response in rails during cross platform response

I have created cross platform form to submit data. It sends data in json format, but rails controller response it as html. So that I always get error as.

    Object {readyState: 0, responseJSON: undefined, status: 0, statusText: "error"}

    test.html?batch[status]=0:88

    Navigated to file:///home/shital/workspace/shitalluitel.github.io/test.html?batch%5Bstatus%5D=0

Here is my rails log after my form submit...

    Started POST "/batches" for 127.0.0.1 at 2017-01-13 08:55:59 +0545
    Processing by BatchesController#create as HTML
      Parameters: {"batch"=>{"name"=>"eight", "course_id"=>"9", "start_date"=>"2016-12-12", "end_date"=>"2016-12-14", "status"=>"1"}}
      User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
       (0.2ms)  BEGIN
      Course Load (0.5ms)  SELECT  "courses".* FROM "courses" WHERE

 "courses"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
  SQL (0.4ms)  INSERT INTO "batches" ("name", "course_id", "start_date", "end_date", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "eight"], ["course_id", 9], ["start_date", Mon, 12 Dec 2016], ["end_date", Wed, 14 Dec 2016], ["created_at", 2017-01-13 03:10:59 UTC], ["updated_at", 2017-01-13 03:10:59 UTC]]
   (133.8ms)  COMMIT
 entered to true part 
Completed 200 OK in 147ms (Views: 0.8ms | ActiveRecord: 135.6ms)

Upvotes: 0

Views: 402

Answers (2)

Hard Developer
Hard Developer

Reputation: 319

you can try this in the controller response will automatically varies according to your platform

respond_to do |format|
  format.html { render 'filename.html'}
  format.json { render json: @batches }
end

Upvotes: 0

bxorcloud
bxorcloud

Reputation: 689

You can use

render json: {your_response: "value"}, status: 200

or if you want nothing then

 render :nothing => true

You can change the status base on you needs.

Upvotes: 1

Related Questions