Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 - How To Render 403 in before_filter w/o DoubleRender Error

I have a before_filter that checks the validity of an API key. If the key is invalid, I'd like to render a header-only 403 response.

In my controller:

before_filter :validate_api
...
def validate_api
  if params[:api_key].present? and ApiKey.find(params[:api_key])
    return true
  else
    render head :forbidden
  end
end

The problem is that I get a DoubleRender error, presumably when Rails goes into the action and attempts to render the response anyways. It was my understanding that Rails prevents execution of the action if a before_filter renders or redirects. Is that not the case?

How do I render a header-only response in a before_filter and prevent actions from being executed?

like image 488
Bryan Marble Avatar asked Nov 10 '11 19:11

Bryan Marble


1 Answers

Have you tried returning false in the else part?

like image 97
Kris Avatar answered Oct 11 '22 21:10

Kris