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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:25:45+00:00 2026-06-13T09:25:45+00:00

I’m a newbie to RoR. The problem I am trying to solve is how

  • 0

I’m a newbie to RoR. The problem I am trying to solve is how to pass an array of hashes from JQuery to Rails for saving in the database.

Context

In my application, a user is able to drag-and-drop any number of items onto a canvas. This is done using JQuery.

Then the user can save these items, including their X-Y coordinates.

Conceptually I believe that this is saving an array of hashes, where each hash corresponds to an item and consists of an ItemCode, X-coordinate, Y-coordinate.

I am trying to figure out at each step, what method/technique should be used.

Here’s what I figured out so far:

  1. ? Save an array of objects in JQuery ?
  2. Serialize this array using jquery.param
  3. Pass the resulting string as params to Rails using jquery.ajax
  4. ?? Rails controller somehow parses this string and saves it in the database??

Key Questions

A. What format should this serialized string take to be easily parsed by Rails?

I believe Jquery.param will generate something like this (in param form):
1[item_code]=432&1[x]=250&1[y]=200&2[item_code]=348… I haven’t been able to find anything to tell me if Rails can figure out that the first 3 terms describe 1 item, the next 3 describe the next item, etc.

B. What is the Rails way to handle this? In other words, what particular methods should I look at as my tools for putting this params string into the database as a several new entries into the database?

And of course, if there’s some completely different way to do this, I’d love to hear it. Thanks.

EDIT

Just to clarify, the ItemCode is meant to be different from the id of the item entry into the database. ItemCode is used to look up characteristics about the item in other tables.

  • 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-13T09:25:46+00:00Added an answer on June 13, 2026 at 9:25 am

    One way:

    decodeURIComponent(jQuery.param({ items: { item1: { x: 1, y: 2 }, item2: { x: 3, y: 4 } } } ))  #  => items[item][x]=1&items[item][y]=2&items[item2][x]=3&items[item2][y]=4
    

    yields a params hash that looks like

    {"items"=>{"item"=>{"x"=>"1", "y"=>"2"}, "item2"=>{"x"=>"3", "y"=>"4"}}}
    

    Then in the controller:

    params[:items].each do |k,v|
      begin
        item = Item.new( id: k, x: v.x, y: v.y )  # or better, Item.new( x: v.x, y: v.y ) 
        item.save
      rescue ActiveRecord::RecordInvalid # deal with problems saving
        ...
        # retry?
      end
    end
    

    In your question, you are generating item ID’s in the client. It’s a better idea to let the DB handle that when you are actually creating the objects to eliminate the need to keep track of unique ID’s. In that case, the ID’s passed to the controller are just arbitrary placeholders, but they do have to be unique in each .param() call to distinguish one object from another.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.