S Craig
S Craig

Reputation: 281

Sending Javascript array to ruby controller on form submit

I have a form with a submit button <%= f.submit "Create Meeting", class: "btn-submit" %> which sends the info collected to my events#create action in my events controller.

I want to be able to send a javascript/JQuery array idList to the controller action at the same time as the rest of the information in the form is submitted. How would I go about doing this? I know it involves an AJAX function, but I don't know when to call it. Do I have to override the submit function in Javascript? If so, how is that done?

Here is how I created the form:

<%= form_for(@newevent) do |f| %>
  ...
  <%= f.submit "Create Meeting", class: "btn-submit" %>
<% end %>

Here is my controller action it submits to:

def create
  @calendar = current_customer.calendar
  @newevent = @calendar.events.build(event_params)

  if @newevent.save
    redirect_to '/main' #'/main/#{@calendar.id}'
  else
    redirect_to '/compose'
  end
end

Any help will be much appreciated, thanks

EDIT: This is the javascript concerning my array. When a user picks a person from a collection select "colleague-select", the selected person's id is added to the array. The id can also be removed by another click action by the user.

var idList = []; //this is the array of entry ids

$(function (){
    $("#click-me").click(function (e){
        e.preventDefault();
        i++;

        var list = $("#list");
        var name = $("#colleague-select option:selected").text();
        var id = $("#colleague-select").val();

        var remove = "<button type='button' class='remove-me'> X </button>";
        var entry = $("<li>" + name + remove+ "</li>").attr('id',id); //creates entry as JQuery object and sets its id attribute

        list.append(entry); //adds entry to HTML element 'list'
        idList.push(id); //adds id to array which should be the same as entry's id

        return false;
    });
});



$(document).on('click', ".remove-me", function(){
    var entry = $(this).parent();
    var id = entry.attr("id"); //gets value of attribute "id".

    entry.remove();

    var index = idList.indexOf(id); //finds the index number of id within array

    idList.splice(index,1); //removes id from array

});

Upvotes: 1

Views: 782

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

Depending on the IDs you want to send, you could use a hidden field storing the IDs:

<%= form_for(@newevent) do |f| %>

  # ...
  <%= hidden_field_tag 'some_ids[]', [1,2,3], id: 'extra_ids' %>
  <%= f.submit "Create Meeting", class: "btn-submit" %>
<% end %>

And then collect the IDs in the params:

def create
  some_ids = params[:some_ids]
  # ...
end

To set it with your Javascript:

$(function (){
    $("#click-me").click(function (e){
        # [...]
        idList.push(id); //adds id to array which should be the same as entry's id

        $('#extra_ids').val(idList);

        return false;
    });
});



$(document).on('click', ".remove-me", function(){
    # [...]
    idList.splice(index,1); //removes id from array
    $('#extra_ids').val(idList);
});

Upvotes: 1

Related Questions