Pregunta

I am consuming a Rails API using a client app written in Rails. If the user is not authenticated then i have to show the login page. I have created a structure of making API calls from only one Ruby class. Now from that Ruby class if the response status is 401 then i have to show login page. Is there any way of redirecting user to login page from my Ruby class?

Here is my client code


class Client
  def execute(method, url, params)
    response = case method
               when :get
                 HTTParty.get(url, query: params)
               when :post
                 HTTParty.post(url, body: params)
               when :put
                 HTTParty.put(url, body: params)
               when :delete
                 HTTParty.delete(url, body: params)
               else
                 nil
               end
    if response.status == 401
      # should redirect to login page
    else
      # Send result to caller which will parse it and send back to the controller action who called it
    end
  end

end

¿Fue útil?

Solución

I solved this problem by raising a custom exception and catching that in ApplicaionController. From there i can redirect to login page easily.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top