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

  • SEARCH
  • Home
  • 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 757001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:18:41+00:00 2026-05-14T15:18:41+00:00

I like HAML . So much, in fact, that in my first Rails app,

  • 0

I like HAML. So much, in fact, that in my first Rails app, which is the usual blog/CMS thing, I want to render the body of my Page model using HAML (obviously I won’t do the same for Comment!). So here is app/views/pages/_body.html.haml:

.entry-content= Haml::Engine.new(body, :format => :html5).render(self)

…and it works (yay, recursion). What I’d like to do is validate the HAML in the body when creating or updating a Page. I can almost do that, but I’m stuck on the scope argument to render. I have this in app/models/page.rb:

validates_each :body do |record, attr, value|
    begin
        Haml::Engine.new(value, :format => :html5).render(record)
    rescue Exception => e
        record.errors.add attr, "line #{(e.respond_to? :line) && e.line || 'unknown'}: #{e.message}"
    end
end

You can see I’m passing record, which is a Page, but even that doesn’t have a controller, and in particular doesn’t have any helpers like link_to, so as soon as a Page uses any of that it’s going to fail to validate even when it would actually render just fine.

So I guess I need a controller view as scope for this, but accessing that from here in the model (where the validator is) is a big MVC no-no, and as such I don’t think Rails gives me a way to do it. (I mean, I suppose I could stash a controller in some singleton somewhere or something, but… excuse me while I throw up.)

What’s the least ugly way to properly validate HAML in an ActiveRecord validator?

  • 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-14T15:18:41+00:00Added an answer on May 14, 2026 at 3:18 pm

    With a little more searching, I’ve found a thread on Google Groups with an idea that gets most of the way there for my purposes:

    base = ActionView::Base.new('/app/views/pages', {}, PagesController) 
    Haml::Engine.new(value, :format => :html5).render(base)
    

    …but as the thread goes on to say, some things, like partials and extra helpers, don’t work.

    Here’s what I ended up using, which seems to cope with everything I’ve thrown at it:

    contr = PagesController.new()
    contr.response = ActionController::Response.new()
    scope = ActionView::Base.new(["#{RAILS_ROOT}/app/views/pages","#{RAILS_ROOT}/app/views"], {}, contr)
    scope.template_format = 'html'
    Haml::Engine.new(value, :format => :html5).render(scope)
    

    (context)

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

Sidebar

Related Questions

How can I do something like this in HAML (within Rails app such that
I love using HAML for HTML documents. It has clean syntax that's much more
I've played with a lot of useful tools like HAML and LESS that gives
I often want some html like this: (<span id=items_shown>all</span> items shown) which I end
I'm using Haml in a Rails 3 app, and its newlines drive me nuts!
Essentially what I want to do is have a root application.haml that contains the
Hi :) I'm new to haml and want something like this: %h1 =link_to Home
I have a line of HAML like this: %li{:id=>node.shortcode, :class=>liclass unless liclass.empty? } which
Hey, all. Working on my first Rails app. I've searched all around - read
I have a form, that looks like this (sorry for Haml and the german

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.