I am doing a POST request to my create action in my controller with a json like this:
{
"token":"amsdaashdaksjdh" #assume this is a valid token for a user
"post": {
"content":"some random text"
"title":"random title"
}
}
However, I have a before_filter check to see if a User is in my database based on the token like this:
before_filter :authenticate
def authenticate
@user = User.find_by_authentication_token(params[:token])
if (@user == nil)
render :json => {error: "invalid token"}
end
end
It is always giving me the invalid token error…
A Rails
before_filterhalts the processing chain when the filter returnsnilorfalse. Because yourifstatement is the last operation executed, the filter will returnnil, when@useris notnil– and this will halt processing.You could add an
elseblock returning true when the user has been found.