Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set response status before render in rails 4

I'd like to set the response status value in particular action methods before the render method is called. Is this not possible?

Many of my methods in controllers render JSON API views for action methods like #destroy, #update, #create and those actions simply invoke #show or #index as is appropriate. However, I'd like to also return the appropriate HTTP Response status value, like 201, 202, etc, without having to pass arguments to these methods. Essentially, I am looking for something like this:

  def destroy
    # code that kills
    status :accepted # ArgumentError, status= silently fails
    index
  end
like image 841
paul Avatar asked Oct 24 '25 17:10

paul


2 Answers

if you just call

response.status = ###

in a controller, and then don't add the :status argument at render, it should get you what you need.

like image 131
whatbird Avatar answered Oct 26 '25 06:10

whatbird


you can use the method render by passing the parameter :status, example :

render nothing: true, status: 201
like image 39
Maroo-b Avatar answered Oct 26 '25 06:10

Maroo-b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!