How to manage response status code from a Rails Controller action

Given the classic action of the controller. Look at the MARK. I need to set the status code 200 to respond. Prerequisites: The swfupload ajax file upload solution seems to be sending data in the wrong format.

I tried response.headers ['Status'] = 200, response.status 200, render: json => 'data' ,: status => 200. But the response status code does not change.

def create
  if params[:Filedata]
    @medium = Medium.new(:swf_uploaded_data => params[:Filedata])
  else
    @medium = Medium.new(params[:medium])
  end

  respond_to do |format|      
    if @medium.save  
      format.html { redirect_to(@medium, :notice => 'Medium was successfully created.'); }
      format.xml  { render :xml => @medium, :status => :created, :location => @medium; }        
      MARK
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @medium.errors, :status => :unprocessable_entity }
    end
  end
end
+3
source share
2 answers

The status code can only be declared in return commands such as render, redirect_to .... and will affect this return command, there is no way to set the code for all responses

format.html { redirect_to(@medium, :notice => 'Medium was successfully created.'); }, , 3XX,

format.xml { render :xml => @medium, :status => :created, :location => @medium; } , , 201, , 200, :

format.xml { render :xml => @medium, :status => :ok, :location => @medium; }

+5

AJAX ? HTML XML, , JavaScript JSON, .

, , , respond_to, . :

if @medium.save
  respond_to { |format| ... }
else
  respond_to { |format| ... }
end

, "MARK".

0

Source: https://habr.com/ru/post/1795027/


All Articles