Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6102691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:38:38+00:00 2026-05-23T13:38:38+00:00

I want to have a rake task that reads a HAML file and creates

  • 0

I want to have a rake task that reads a HAML file and creates a static html file out of it. The reason for this is that I want to dynamically localize my error pages in a manner described here http://devcorner.mynewsdesk.com/2010/01/13/rails-i18n-and-404500-error-pages/

Here is the method for writing the error pages.

def write_error_page(status, locale = nil)
  dest_filename = [status.to_s, locale, "html"].compact.join(".")
  File.open(File.join(Rails.root, "public", dest_filename), "w") do |file|
    path = File.join(Rails.root, "app", "views", "errors", "#{status}.haml")
    file.print Haml::Engine.new(File.read(path)).render
  end   
end

The problem is that Haml::Engine does not have rails methods available. So when a try to read the haml file, I get an error for every rails method in the file (I want to use methods
like image_tag, form_for and obviously I18n.translate).

I noticed a similar issue that had been solved here: Rails HAML engine rendering

However, when I try the solution mentioned in the link above, I get the following error: “undefined local variable or method `config’ for #”.

How could I get the rails methods to work in the Haml::Engine so that I could read the HAML file? I also tried switching to ERB, but noticed that it leads to the same problem, which somebody else has at least partially resolved here render erb from database into view problem please help! But this solution didn’t help me either.

I’m also open to other solutions than using Haml::Engine. I looked into capture_haml helper but don’t see how that would help me either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:38:39+00:00Added an answer on May 23, 2026 at 1:38 pm

    I just now realized that I don’t need Haml::Engine in this situation, because I’m in a rails environment so I can just call render. Silly me.

    However, it’s not completely trivial to call render from a rake task, because we are not in a controller or a view (and so rails purists even say that you should never do so, I think, but in this case it seems like the easiest way), so I post the code I used here (I used the approach mentioned here: http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/).

    def write_error_page(status, locale = nil)
      dest_filename = [status.to_s, locale, "html"].compact.join(".")
      File.open(File.join(Rails.root, "public", dest_filename), "w") do |file|
        path = File.join("app", "views", "errors", "#{status}.haml")
        file.print ActionView::Base.new(Rails.configuration.paths.app.views.first).render(:file => path)
      end   
    end
    

    I had some problems with this approach too. For instance, form_for still didn’t work properly (I want to have a feedback form on the error page) so I simply created the form with plain HTML (which you can luckily inject straight into .haml files). But the one thing from rails I needed to get to work in the .haml template – method I18n.translate – works like charm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails rake task which runs just fine. I want this task
I want to have an executable file that will call some other programs. The
I have a rake task that populates some initial data in my rails app.
I want to use a Rake task to cache my sitemap so that requests
I want to call a rake task from a cron job that stores remote
I have install gem rak i want to use rak command inside rake task
I want to have a select-only ComboBox that provides a list of items for
I want to have a text box that the user can type in that
I want to have a function that will return the reverse of a list
I want to have a map that has a homogeneous key type but heterogeneous

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.