This is really starting to drive me crazy; I’ve spend a solid two hours on this and I just can’t figure it out! What I am trying to do is use ERB in a YAML file to include a rails route in the YAML file. Long story short this will be read back later to make a semanic menu from the YAML file. I am loading the YAML file (with ERB) in an initializer _load_config.rb and the code which loads it looks like this:
menu_yml= "#{Rails.root}/config/menu.yml"
config = YAML.load(ERB.new(IO.read(menu_yml)).result)
MENU_CONFIG = config
In the YAML file menu.yml I have something to the effect of:
logged_in:
dashboard:
text: "Dashboard"
url: <%= url_for dashboard_path %>
The problem is that <%= url_for hasboard_path %> throws and error:
(erb):4:in `<main>': undefined local variable or method `dashboard_path' for main:Object (NameError)
From much debugging I presume that this is due to routes being loaded after this initializer. Any suggestions as to how to fix this is much appreciated!
P.S. This is using Rails 3.1.rc5
The reason you are getting that error is that
url_foris a helper for ActionView and therefore not accessible when the initializer runs – it won’t work if called from models or the console, so the problem isn’t necessarily that routes are being loaded after that initializer.I’m not sure exactly how you’re dashboard route is setup and why it needs to be dynamically tied to
menu.yml. Assuming it doesn’t have any params sent through the URL(since no args are passed into url_for’) and you are usingmatchfor routing: Having a global or environment variable that is referenced byroutes.rbandmenu.ymlmight give the functionality you need.boot.rb
routes.rb
menu.yml