Search code examples
ruby-on-rails-3rails-routingrails-console

How can I run a Controller Action from Rails Console, but setting the request.shot?


I'm working on a multi-tenant application, so pretty much everything throughout (starting with routing) expects to have a "request.host" that looks like "tenant_id.myapp.com".

I'm trying to run a controller action from the Rails console, but I haven't been able to figure this one out.

The easiest thing to do seems to be "app.get", but I can't figure out how to set the host.

I also saw an answer that mentions using "ActionController::TestProcess", which as far as I understand has been removed from Rails (or if not, I haven't found how to include it)

Any other options?

Thanks!
Daniel


Solution

  • I just verified it in my console(Pry)

    # Send request with fake HTTP_HOST
    >>app.get(app.root_path, nil, {"HTTP_HOST" => "123.myapp.com"})
    >>#=>200
    
    # Then check if it works
    >>app.request.env
    >>#...
    >># "HTTP_HOST" : "123.myapp.com"