在跨平台响应期间更改对rails中的json响应的html响应

时间:2017-01-13 03:47:19

标签: ruby-on-rails json

我创建了跨平台表单来提交数据。它以json格式发送数据,但rails控制器将其作为html响应。所以我总是得到错误。

    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

这是我的表单提交后的rails日志...

    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)

2 个答案:

答案 0 :(得分:0)

您可以使用

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

或者如果你什么都不想,那么

 render :nothing => true

您可以根据需要更改状态。

答案 1 :(得分:0)

您可以在控制器中尝试此操作,响应将根据您的平台自动变化

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