Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on a Rails 3.2.1 app and would like to pass some Javascript variables to the Rails controller. Here are some more details:

my js fiddle: http://jsfiddle.net/PauWy/475/

and what fiddle doesn't show, a jQuery UI sortable: $("#menu-items").sortable();

I would like to work with the following information in my Rails 'execute' method in the ThingsController for each item:

  1. Whether or not the item is visible (I think I should do something like $('#item_1).is(':visible')
  2. Which one of the following is displayed - "Add", "Reduce", or neither
  3. the id of the item
  4. the order/position of the item in the sortable list

My take would be to send an AJAX with the above data to the 'things/execute' action, and somehow route it and respond_to the incoming AJAX.

I know vaguely how some bits and pieces should look like, but I'm having a hard time putting everything together. Would really appreciate some help!

share|improve this question
    
On a side note, please upgrade to the latest 3.2.x release before putting this on an internet site, or it will get hacked. –  DGM Jul 19 '13 at 1:04

1 Answer 1

up vote 0 down vote accepted

So, you need to parse you HTML to get information from it

Example

HTML :

<div id="my_content">
  <div id="div1" class="reduce" style="visibility: visible;"></div>
  <div id="div2" class="add" style="visibility: visible;"></div>
  <div id="div3" class="add" style="visibility: visible;"></div>
  <div id="div4" class="reduce" style="visibility: visible;"></div>

  <div id="div5" class="reduce" style="visibility: hidden;"></div>
  <div id="div6" class="add" style="visibility: hidden;"></div>
  <div id="div7" class="add" style="visibility: hidden;"></div>
  <div id="div8" class="reduce" style="visibility: hidden;"></div>
</div>

Then you can parse it to JSON (using jQuery) :

Javascript :

var parsed_data = {}
var order = 1

$('#my_content > div').each(function() {
  // Some if else
  var element_class = $(this).hasClass('reduce') ? 'reduce' : 
                    ( $(this).hasClass('add')    ? 'add' : 'unkown')

  parsed_data['data'+ order] = {
    element_id:    $(this).attr('id'),
    display_class: element_class,
    visibility:    $(this).css('visibility'),
  }

  order++
})

Then you call your controller with AJAX :

$.ajax({
  url: "<CONTROLLER_URL>",
  type: "POST",
  dataType: 'json', // data returned, not data sent
  data: parsed_data
}).done(function(data) {
  console.log('Done')
})

Ruby Controller :

def your_controller_function
  params.each do |key, div|
    print "DIV N°#{key}"
    print "  Id #{element_id}"
    print "  Displayed class #{display_class}"
    print "  Visibility #{visibility}"
  end
end

I hope it's not too messy and it helped.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.