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 4055598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:43:16+00:00 2026-05-20T14:43:16+00:00

We are using Mustache templates and I would like to make a preview View

  • 0

We are using Mustache templates and I would like to make a preview View in our RoR web application that combines a template and some data we have stored in our database, but it does not work as I expected and after some searching on the internets (including SO!), I didn’t find any examples that included an active model.

How do you pipe the ActiveModel records to Mustache to merge with a template?

The setup:

The Schema

create_table "templates", :force => true do |t|
  t.string   "kind"
  t.text     "data"
  t.integer  "data_count"
end
create_table "bars", :force => true do |t|
  t.string   "guid"
  t.string   "name"
  t.string   "summary"
end

There is nothing special about the models. Both are subclassed from ActiveRecord::Base

class Bars < ActiveRecord::Base
end
class Templates < ActiveRecord::Base
end

The Controller

class TemplateController < ApplicationController
  def preview
    @result = Mustache.render( template.data, :bars => Bar.limit(template.data_count ) ).html_safe
  end
end

The View

<%= @result %>

The Route

get 'templates/:id/preview' => 'templates#preview', :as => 'templates_preview'

The Data

y Bar.all

--- 
- !ruby/object:Bar
  attributes: 
    guid: "1"
    name: "test1"
- !ruby/object:Bar
  attributes: 
    guid: "2"
    name: "test2"

The Template (i’ve simplified the html for example purposes)

<html>
<head>
</head>
<body>
  {{#bars}}
    <a href="{{guid}}">{{name}}</a>
  {{/bars}}
</body>
</html>

The Result

<html>
<head>
</head>
<body>
    <a href=""></a>
</body>
</html>

The Expectation

<html>
<head>
</head>
<body>
    <a href="1">test1</a><a href="2">test2</a>
</body>
</html>

I am hoping there is an easy answer to this and I am just missing it. Thanks.

  • 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-20T14:43:17+00:00Added an answer on May 20, 2026 at 2:43 pm

    Does it work if you change your controller to:

    @result = Mustache.render( template.data, :bars => Bar.limit(template.data_count).all ).html_safe
    

    (added a call to .all after Bar.limit(template.data_count))

    I’m pretty new to Mustache, but glancing very quickly through the code seems to show that it calls this for a section:

    v = [v] unless v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator)
    

    Bar.limit(template.data_count) returns an ActiveRecord::Relation, which is neither an Array nor an Enumerator. Calling .all on the relation turns it into an array and should cause Mustache to pass it into the section accordingly.

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

Sidebar

Related Questions

After becoming fond with mustache.js template-style, I would like continue using it in node.js.
I would like to separate the template from the data using mustache.js in node...
I'm using Flask to make a web app and using mustache templates (alongwith sammy.js).
I am using stache for server-side evaluation of Mustache templates. I would like to
I'm using mustache.js to render a template in javascript. I'd like to check if
I would like to use a single haml/mustache template, like this: .foo %h2 {{title}}
I need to add mustache templates to a Twig template using symfony2. Someone has
Currently I'm using Mustache templates ( http://mustache.github.com/ ) both on the client and server-side,
Using Rails 3.2.0 with haml and sass: I Would like to link an external
I am following the tuturoial here: http://backbonetutorials.com/organizing-backbone-using-modules/ and all I would like to do

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.