2015-06-09 19 views
0

我創建了一個關於產品的應用程序,它有namepricedescription。在index.html.erb,我想添加文本框到他們每個人,點擊編輯鏈接可以編輯不需要去/產品/編號/編輯。如何在rails上的ruby中更新index.html.erb中的數據?

如何編輯產品數據?

<% @products.each do |product| %> 
     <tr> 
     <form action="/products/<%= product.id %>" class="edit_person_<%= product.id %>" id="edit_person_<%= product.id %>" method="post"> 
      <input name="_method" type="hidden" value="patch" /> 
      <input name="authenticity_token" type="hidden" value="NrOp5bsjoLRuK8IW5+dQEYjKGUJDe7TQoZVvq95Wteg=" /> 
      <td><input id="product_name" name="product[name]" type="text" value="<%= product.name %>" /><br /></td> 
      <td><input id="product_price" name="product[price]" type="text" value="<%= product.price %>" /><br /></td> 
      <td><input id="product_descr" name="product[descr]" type="text" value="<%= product.descr %>" /><br /></td> 
      <td><input name="commit<%= product.id %>" type="submit" value="edit" /></td> 
     </form> 
     </tr> 
    <% end %> 
+0

什麼是你控制器是什麼樣子? – locoboy

+0

你可以通過使用AJAX來做到這一點 –

+0

你可以告訴我如何使用AJAX? –

回答

0

你需要,如果你想在不重裝edit.html.erb文件更新您的數據使用AJAX。下面,我將向你展示如何爲一個領域做好準備,休息你可以假設你自己。

<input id="product_price" name="product[price]" type="text" value="<%= product.price %>" /> 

在你js.erb文件,你可以這樣寫:

$("#product_price").focusout(function() { 
    var productPrice = $(this).val(); 
    $.ajax({ 
     url: "/products/<%= product.id %>/edit", 
     type: "POST", 
     data: { product_price: productPrice}, // same way, other attributes 
     success: function() { 
      console.log("The result successfully came back from ther server."); 
      // Now, here you can show a notice to the user that he has successfully update the record without reloading the page. 
     } 
    }); 
}); 
+0

我試着用這個ajax做但不工作 –

+0

你得到了什麼錯誤?你能告訴我你的代碼嗎? –

相關問題