[英]Access controller action variable in jquery and display it in ruby on rails
[英]Rails/jquery passing a variable(IDs) collected in jQuery to a rails controller/action
我是AJAX和jQuery的新手,所以請原諒我,如果這是基本的:
我用2dconcepts( http://www.2dconcept.com/jquery-grid-rails-plugin )實現了很棒的jgrid。 現在我想用復選框選擇表格中的數據, - 獲取產品ID(工作正常我看到來自'handleSelection'的警報)並將其傳遞給我在rails控制器中的自定義操作以編輯指定的記錄(非常類似於Ryan B的railscast#165)。 我根本不知道我會怎么做。
<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 }
)%>
我想我需要將post-request放在函數中並用這樣的方式調用它:
<%= button_to_function('EDIT CHECKED', 'handleSelection(id)', {
:id => "products_select_button"}) %>
但說實話,即使該按鈕也不起作用,因為它將字符串“products_select_button”傳遞給函數,而不是收集的值...
非常感謝您的幫助!
瓦爾
“products_select_button”})%>要將參數發送到控制器,您始終可以使用jQuery的post函數,在任何設置中都可以輕松實現
$.post("products/", { id: 1, name: "John" } );
這段代碼使用POST將id和name參數發送到您的產品控制器。 根據控制器的外觀,不同的操作將接收POST。 你總能這樣做
rake -T
在你的rails應用程序中,以查看處理POST的操作。 我真的不知道最終的網格是什么樣的,但這段jQuery代碼可能會讓你入門?
$(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
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.