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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:14:07+00:00 2026-06-15T07:14:07+00:00

I’m using jQuery Autocomplete with categories, I’ve used the demo code which worked fine,

  • 0

I’m using jQuery Autocomplete with categories, I’ve used the demo code which worked fine, now i’m trying to populate the drop down with data from my database. I’ve used this code within my view:

:javascript
  $(function() {
      var data = [
          - Datasheet.find(:all).each do |ds|
            { label: "#{ds.title}", category: "Data Sheets" },
          - Brochure.find(:all).each do |br|
            { label: "#{br.title}", category: "Brochures" },

        $( "#search" ).catcomplete({
          delay: 0,
          source: data
      });
  });

but I get the error undefined local variable or method 'ds' for #<#<Class:0x00000005c95a18>:0x00000005c8d570>

I’m not sure if it’s because i’m mixing haml with javascript? is there another way around this? All I have in my view is a text field called search, which works with demo code.

  • 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-06-15T07:14:08+00:00Added an answer on June 15, 2026 at 7:14 am

    Code inside a filter such as :javascript isn’t treated as Haml, it’s just s string that gets fed through the filter, so - and = don’t work as they do in the rest of Haml. You can use interpolation (i.e. #{...} blocks) though. In fact that’s what’s causing your error – in "#{ds.title}" Haml is trying to evaluate the ds, but not in the context of the block passed to Datasheet.find.

    #{} blocks can span multiple lines, so you could change your code to something like this:

    :javascript
      $(function() {
          var data = [
              #{
              (Datasheet.find(:all).map do |ds|
                "{ label: \"#{ds.title}\", category: \"Data Sheets\" }"
              end +
              Brochure.find(:all).map do |br|
                "{ label: \"#{br.title}\", category: \"Brochures\" }"
              end).join(',')
              }
          ];
    
          $( "#search" ).catcomplete({
            delay: 0,
            source: data
          });
      });
    

    Here instead of using each to iterate over each array we use map to create a new one, then use + to add the two arrays together and finally use join(',') to combine them into a single string which is inserted into the page.

    This is a pretty ugly and unwieldy bit of code however. A better solution in this case would be to extract out the code that creates the array into a helper and call the helper from your Haml.

    Helpers:

    def datasheet_array
      Datasheet.find(:all).map do |ds|
        "{ label: \"#{ds.title}\", category: \"Data Sheets\" }"
      end
    end
    
    def brochure_array
      Brochure.find(:all).map do |br|
        "{ label: \"#{br.title}\", category: \"Brochures\" }"
      end
    end
    
    def array_for_js
      "[#{(datasheet_array + brochure_array).join(',')}]"
    end
    

    Then your Haml can be the much clearer:

    :javascript
      $(function() {
          var data = #{array_for_js};
    
          $( "#search" ).catcomplete({
            delay: 0,
            source: data
          });
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.