New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
config.after_routes_loaded defined on config/application.rb never called #50720
Comments
This occurs on Ruby 3.2.2 also. I was testing to see if this happens in Rails 7.0. However I get an error:
Test case: # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', '7.0.0'
gem 'timeout', '0.4.1'
# If you want to test against edge Rails replace the previous line with this:
end
require "action_controller/railtie"
AFTER_ROUTES_LOADED_CALLBACK_CALLED = false
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.after_routes_loaded do
::AFTER_ROUTES_LOADED_CALLBACK_CALLED = true
end
routes.draw do
get "/" => "test#index"
end
end
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
def index
render plain: "Home"
end
end
require "minitest/autorun"
require "rack/test"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_returns_success
get "/"
assert last_response.ok?
end
def test_after_routes_loaded_callback_called
assert AFTER_ROUTES_LOADED_CALLBACK_CALLED,
'after_routes_loaded was never called'
end
private
def app
Rails.application
end
end Dockerfile: # Use an official Ruby runtime as a parent image
FROM ruby:3.2.2
RUN gem update --system
RUN gem install bundler
RUN gem install stringio -v 3.1.0
RUN gem install psych -v 5.1.2
# Install SQLite3
RUN apt-get update -qq && apt-get install -y sqlite3 libsqlite3-dev
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the script into the container at /usr/src/app
COPY . .
# Run the script when the container launches
CMD ["ruby", "test.rb"] |
I'm pretty sure this works because I use it every day. Let me try in a new app. |
Yes. It does work, just not in the way you are expecting. In your test script there is no routes being reloaded. It only runs when they are reloaded, not when routes are first loaded. Now, if it should also run when routes are first loaded is a good question. @shioyama do you remember? |
@rafaelfranca Yes, I can confirm it works on reload, but since the name is *_loaded and the guides says:
https://guides.rubyonrails.org/configuring.html#config-after-routes-loaded I just assumed it was supposed to run on both cases (I need to overwrite some helpers on my application globally, and it must work on load or reloads). To circumvent the issue, I forced triggering it in the after_initialize, but this doesn't seem to be the best/proper way to do it:
|
Right. I'm just trying to figure out if it is the name that is wrong, or it is the implementation that is wrong. |
Sorry, it seems that the behaviour is wrong here. It should run after the routes are loaded in boot. I was thinking too narrowly of our own use case. |
This hook was only running when routes were reloaded, but not on boot. The goal was to run any time routes are loaded. This commit fixes it and adds a test. Fixes #50720.
We just upgraded our application to 7.1.3 and it is working as expected. Thank you! |
Steps to reproduce
config.after_routes_loaded
inside the config/application.rbExpected behavior
The block should be called after application routes are loaded (including on each possible reload of the routes)
Actual behavior
The block is never called
System configuration
Rails version: tested against both 7.1.2 and edge
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered: