Undefined local variable or method `blog'

I’m trying to add atom feeds for each tag, and the common way to achieve this is by adding the following code to config.rb:

ready do
  blog.tags.each do |tag, articles|
    page "/tags/#{tag}/#{tag.downcase}.xml", proxy: "/atom.xml", layout: false do
      @tagname = tag
      @articles = articles[0..10]
    end
  end
end

I put that just underneath my activate block for middleman-blog:

activate :blog do |blog|
  blog.permalink = '{year}/{month}/{day}/{title}.html'

  blog.tag_template = 'tag.html'
  blog.calendar_template = 'calendar.html'
end

So with just the the active block, it works perfectly. However, as soon as I hit the above ready do block, building fails:

Building blog (blog.foo.bar)
/home/red_trela/.gem/ruby/gems/middleman-blog-4.0.0/lib/middleman-blog/blog_data.rb:170: warning: duplicated key at line 170 ignored: :lang
== Blog Sources: {year}-{month}-{day}-{title}.html (:prefix + :sources)
/home/red_trela/git/mathys.io/blog/config.rb:92:in `block in evaluate_configuration!': undefined local variable or method `blog' for #<Middleman::ConfigContext:0x0056307e1869d8> (NameError)
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:51:in `instance_exec'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:51:in `block in execute'
        from /home/red_trela/.gem/ruby/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `each'
        from /home/red_trela/.gem/ruby/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `traverse_depth_first'
        from /home/red_trela/.gem/ruby/gems/hamster-2.0.0/lib/hamster/vector.rb:431:in `each'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:51:in `execute'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:28:in `block in install_methods!'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:52:in `instance_exec'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:52:in `block in execute'
        from /home/red_trela/.gem/ruby/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `each'
        from /home/red_trela/.gem/ruby/gems/hamster-2.0.0/lib/hamster/vector.rb:1316:in `traverse_depth_first'
        from /home/red_trela/.gem/ruby/gems/hamster-2.0.0/lib/hamster/vector.rb:431:in `each'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:52:in `execute'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/callback_manager.rb:28:in `block in install_methods!'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/application.rb:292:in `start_lifecycle'
        from /home/red_trela/.gem/ruby/gems/middleman-core-4.0.0/lib/middleman-core/application.rb:265:in `initialize'
        from /home/red_trela/.gem/ruby/gems/middleman-cli-4.0.0/lib/middleman-cli/build.rb:51:in `new'
        from /home/red_trela/.gem/ruby/gems/middleman-cli-4.0.0/lib/middleman-cli/build.rb:51:in `build'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:115:in `invoke'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor.rb:40:in `block in register'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
        from /home/red_trela/.gem/ruby/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
        from /home/red_trela/.gem/ruby/gems/middleman-cli-4.0.0/bin/middleman:23:in `<top (required)>'
        from /home/red_trela/bin/middleman:23:in `load'
        from /home/red_trela/bin/middleman:23:in `<main>'

Idead? Any help is appreciated.

Did you fix this? I have the same issue.

Hm, I don’t remember this well, but I guess that’s the “fix” / new way of doing the same:
https://github.com/red-trela/mathys.io/blob/master/blog/config.rb#L38-L70

1 Like

This absolutely needs to be documented somewhere! I couldn’t find any reference to this on the migrating from Middleman 3 list.

I’m updating an old blog and spending a long time playing around with Jekyll, this was basically the feature keeping me on Middleman and now it’s broken with the only fix documented here.

Hey hey! I’m actually running into this (in 2021, if you can believe) and can’t see your solution. If there’s any way you could help out, I’d really appreciate it!

The file has moved here, but that’s all that I remember. Not sure this still works either, I haven’t verified thos in years.