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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:48:46+00:00 2026-06-15T13:48:46+00:00

I need to create and then pass an array/hash from Rails into Javascript through

  • 0

I need to create and then pass an array/hash from Rails into Javascript through a data tag.

The Javascript needs to be formatted like this:

[{x: 1333065600000, title: 'Title', text: 'Text'}, {x: 1333065600000, title: 'Title', text: 'Text'}]

I created a helper method in Rails to create this type of syntax:

def flags
  @flags = '['
  @model.flags.each do |f|
    @flags += "{ x: 1333065600000, title: '#{f.title}', text: '#{f.text}'},"
  end
  @flags += ']'
end

The data is then passed into Javascript like so:

<%= content_tag 'div', '', id: 'container', data: {flags: flags} %>

The HTML source code looks correct, but the ‘s are encoded. Despite this, the div shows up without any errors, but also without any flags. Inputting the exact result of this directly into the Javascript shows the flags properly.

I know that the data tags are automatically JSON encoded, so I also tried using the standard HTML data tag with ERB tags nested in them, but came across the same problem.

Does anyone know what the problem might be? Any help would be greatly appreciated!

  • 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-15T13:48:47+00:00Added an answer on June 15, 2026 at 1:48 pm

    This is a cleaner way to write your helper, returning the array instead of a contrived JSON string.

    def flags
      @flags = []
      @model.flags.each do |f|
        @flags << { x: 1333065600000, title: f.title, text: f.text }
      end
      @flags
    end
    

    I’m not sure what you’re trying to accomplish, as

    <div data-flags="[{&quot;x&quot;:1333065600000,&quot;title&quot;:&quot;some title&quot;,&quot;text&quot;:&quot;some text&quot;}]" id="container"></div>
    

    is expected output. You need to decode the data-flags attribute before parsing it as a JSON encoded string.

    If for some reason you’re wanting single quotes instead of the double quotes, you can do this with

    content_tag 'div', '', id: 'container', data: { flags: raw(x.to_json.gsub(/\"/, "'")) }
    

    This will produce

    <div data-flags="[{'x':1333065600000,'title':'some title','text':'some text'}]" id="container"></div>
    

    which I assume is what you’re after.

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

Sidebar

Related Questions

I need to create an NSImage from a url and then set it to
I need to create a bash shell script starting with a day and then
In my activity, I create a Bitmap object and then I need to launch
I created a branch and then made changes to trunk which need merging into
I need create custom dialog and put JPanel into it. Is it possible?
I need to pass an array of strings as parameter to a MySQL stored
I need to send a JavaScript array via querystring to my .ashx. In my
I am trying to pass values from a multiple select listbox through Ajax to
I need to change the size of an array, but I cannot simply create
I need to pass a 1d array that isn't defined in a method. I

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.