So this looked very simple to me but apparently I'm unable to solve this.
In my Rails application, I need to do user authorization.
So, currently in my application if user is unauthorized to view the resource I am redirecting user to his home page with a msg: You are not allowed
. This approach returns HTTP 302.
I want to return 403 from my controller and later in Browser Javascript I want to handle if http status code is 403 then redirect to home page.
I found this answer but it is also doing same redirection.
Current logic:
def authorize
unless user_has_access?
redirect_to home_page, :notice => 'if dont have access to that resource'
end
end
but this return HTTP 302 for the request, ideally which should have been 403.
I want something like this:
Controller
def authorize
unless user_has_access?
flash[:notice] = 'not allowed'
return {status: 403, msg: 'nunauthorized', redirect_url: home_page}
end
end
and in handle it some how in JS:
if status == 403
window.location(redirect_url)
end
- Is this possible in rails ?
- Is there any gem for this ?
- Will
flash[:notice]
still be available in redirected url.