dimanche 13 mars 2016

Converting a JS POST into ruby on rails "controller" code

I am working on a ruby on rails website that also has javascript.

I have this javascript code, that I made, that makes a POST to the website and completes the action that I want it to.

  function create_group_item(group_id, items_id){
    $.ajax({
      type: "POST",
      url: '/group_items/create/',
      data: { "group_id": group_id, "items_id": items_id },
      success: function(data, textStatus, jqXHR){

      },
      error: function (jqXHR, textStatus, errorThrown){
        console.log(errorThrown);
      }
    });
  }

This works and is fine.

The issue I have is calling that same POST function from another rails controller instead of with javascript. I want to create a group_item from inside the item controller.

I have a variable in my item controller: group_item_info = {"group_id" => group_id.to_i, "item_id" => item_id} Which, when I print it reads out: {"group_id"=>15, "item_id"=>754} in some instances. So, I have no issue getting the values of the group and item.

QUESTION How do I call the group_items create function from the items controller.

Attempts so far code such as:

@group.items << @item

and

def create_group_item
    redirect_to url_for(:controller => :group_items_controller, :action => :create)
end

then: create_group_item({group_id: group_id, items_id: item_id})

have only given me errors.

Aucun commentaire:

Enregistrer un commentaire