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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:26:48+00:00 2026-06-17T20:26:48+00:00

I am working on a rails app and I want to create a dynamical

  • 0

I am working on a rails app and I want to create a dynamical amount of variables to pass to my controller via an AJAX request. That is, I want to create 3 variables if there are only 3 objects and 5 variables if there are 5 objects.

In my javascript file, I’ve already managed to create a dynamic amount of variables.

var count = parseInt("#{@matches.count}");

while(count>0){
  eval("result_" + count + "= $('input:radio[name=result_" + count + "]:checked').val()");
  count -= 1;
}

This creates result_1, result_2, result_3, etc.

The AJAX request I have so far is static in that it always has to have 5 results. I want this to be dynamic.

$.ajax({
  type: 'GET',
  url: '/mt_results/create',
  dataType: 'json',
  data: {
    'result_1' : { matched_id: "#{@matches.first.id}", result: result_1 },                                             
    'result_2' : { matched_id: "#{@matches.second.id}", result: result_2 },
    'result_3' : { matched_id: "#{@matches.third.id}", result: result_3 },
    'result_4' : { matched_id: "#{@matches.fourth.id}", result: result_4 },
    'result_5' : { matched_id: "#{@matches.fifth.id}", result: result_5 }
  },
  success: function(e){
    console.log("AWWWWWWW YEAH!!");
  }
});

Is the best way to approach this to dynamically create the JSON object (similar to how I created results_#) and assign it to a variable (let’s call it data_var). Then, I can just pass data_var like below:

data: data_var

Any tip or feedback on how I can improve my code 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-17T20:26:49+00:00Added an answer on June 17, 2026 at 8:26 pm

    In my opinion, when you write JS, you should not use inline ruby unless absolutely necessary. Most of the times, it’s a sign of bad design and produces code that is tightly coupled to your server-side logic.

    It’s better to store the data you need in your tag attributes – or use those that already exist.

    in your case, you could do :

    <% @matches.each do |match| %>
       <%= check_box_tag :my_button_group, false, value: match.id %>
    <% end %>
    

    (btw, you should not use radio buttons when you allow multiple choices – use checkboxes)

    if you really need to add more data, you can do it the html5 way by adding this to the options :

    data: {custom-value: "foo", another-one: "bar"},
    

    which would add these attributes to the tag :

    <input ... data-custom-value="foo" data-another-one="bar" />
    

    Now – you want to analyze these boxes and send an ajax request to whatever. Why even bothering analysis ? Rails gives you a simple way to handle remote forms. Just wrap your boxes in a remote form, submit it on click, and there you go – it even degrades gracefully !

    If you have something more difficult to perform (with data-attributes…), crafting yourself the ajax call data is always possible :

    var boxes = $("input:checkbox[name=my_button_group]:checked");
    var data  = boxes.map(function(box, index){ 
                    return { matched_id: box.val(), result: box.attr("data-custom") };
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm on my second app with Rails and I want to get ajax working.
I am working on a Rails app that allows you to create a configuration
Im working with Rails 3.0.3 I want to create bills in my App. Each
I'm working on a Rails app that sends data through a form. I want
I've got Ajax working on a create action in my Rails 3 app, so
I am trying to get some Javascript working in my Rails app. I want
I did heroku create and pushed a working rails app to the server. Everything
I'm working on a Rails app that implements some social network features as relationships,
I'm working on a Rails app that existing users can invite additional members to
I'm working on a Rails app that will contain information on a bunch of

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.