Rails / jquery passing the variable (ID) compiled in jQuery to the rails controller / action

I am new to AJAX and jQuery, so please excuse me if this is pretty simple:

I implemented a big jgrid on 2dconcepts ( http://www.2dconcept.com/jquery-grid-rails-plugin ). Now I would like to select the data in my table using the checkbox - get the product identifiers (which works fine, I see a warning from "handleSelection") and pass it to my user action in the rails controller to edit the specified record (very similar to Ryan B railscast # 165). I just don’t know how I will do it.

<script type="text/javascript"> function handleSelection(id) { alert('Open those up?:'+id); } </script> <% title "JGRID Table" %> <%= jqgrid("Products", "products", "/products", [ { :field => "id", :label => "ID", :width => 40, :resizable => false }, { :field => "vendorname", :width => 200, :label => "vendorname", :editable => true }, { :field => "productname", :width => 230, :label => "productname", :editable => true }, { :field => "metakeyword", :width => 250, :label => "metakeyword", :editable => true }, { :field => "status", :width => 100, :label => "status", :editable => true, :edittype => "select", :editoptions => { :value => [["inbox","inbox"], ["todo", "todo"], ["final","final"]] } }, { :field => "category_id", :label => "category_id", :width => 100, :resizable => false, :editable => true } ], { :add => true, :edit => true, :inline_edit => true, :delete => true, :edit_url => "/products/post_data", :rows_per_page => 30, :height => 270, :selection_handler => "handleSelection", :multi_selection => true } )%> 

I think I need to put a post request into a function and call it something like this:

 <%= button_to_function('EDIT CHECKED', 'handleSelection(id)', { :id => "products_select_button"}) %> 

But honestly, even this button will not work, because it passes the string "products_select_button" to the function, not the collected value ...

Your help is much appreciated!

Shaft

"products_select_button"})%>
+4
source share
1 answer

To send parameters to your controller, you can always use the jQuery post function, it is very easy to implement it in any setting

 $.post("products/", { id: 1, name: "John" } ); 

This piece of code sends the id and name parameter to your product controller using POST. Depending on how your controller looks, various actions will receive POST. You can always do

 rake -T 

in your rails application, to see what action the POST processes. I really don't know what the final grid looks like, but can this piece of jQuery code run you?

 $(document).ready(function() { $("#button").click(function() { // Submit button is pressed var grid_id = $("#grid").val(); // Grab value of the grid $.post("products/", { id : grid_id }); // Post the grid_id to your controller }); }); 
+1
source

Source: https://habr.com/ru/post/1300776/


All Articles