Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering an "internal" Rails controller from a middleware

I have a Rails middleware stack, and I have a piece of MW outside ActionDispatch. Ideally I would like to render a page using ActionDispatch by triggering a URL which is internal (not accessible via usual URL routes) - similar to the way Devise renders it's "auth failed" pages. The best thing would be to just trigger one specific controller action in the application by name, and return it's render result (without having it in the routes even).

What is the standard, modern way of doing this?

UPDATE:

def call(env)
  if user_from_env(env).free_accout?
    InterestingPagesController.action(:how_to_signup).call(env)
  else
    @app.call(env)
  end
end
like image 435
Julik Avatar asked Mar 07 '26 21:03

Julik


1 Answers

You can return a controller action as a Rack endpoint using controller.action, then call the endpoint with endpoint.call() or endpoint[].

like image 111
Noah Gibbs Avatar answered Mar 10 '26 15:03

Noah Gibbs