2014-11-21 29 views
0

我有Rails 4.0應用程序與'訂單'模型有一個狀態字段。在調用「顯示」操作以獲取「訂購」對象細節時,除了「編輯」和「返回」之外,我還想在「顯示頁面」上添加其他按鈕。第一個標記'更新狀態'只會將這個訂單對象的狀態更新爲'1',第二個標記'關閉訂單'的按鈕只會將這個訂單對象的狀態更新爲2.如何在沒有'編輯'動作/視圖?我是否需要使用form_tag來添加這些按鈕,並且是否需要在OrdersController中編寫2個新方法來更新狀態?Rails更新一個沒有表單的字段

以下是我show.html.erb有兩個按鈕我要補充...

<p> 
    <strong>Name:</strong> 
    <%= @order.name %> 
</p> 
<p> 
    <strong>Status:</strong> 
    <%= @order.status %> 
</p> 
<%= link_to 'Edit', edit_order_path(@order) %> | 
<%= link_to 'Back', orders_path %> | 
<%= link_to 'Shipped', '#', :name => 'shipped_button' %> | 
<%= link_to 'Close', '#', :name => 'close_button' %> 

如果我點擊「已發貨」或「關閉」按鈕,它們都將在爲「OrdersController」被稱爲如,如下所示一個 'GET' 請求,

.. 發起者GET 「/命令/ 1?名稱= shipped_button」 爲127.0.0.1在2014年11月21日15點40分38秒-0800
處理OrdersController#顯示爲HTML
參數:{「name」=>「shipped_button」,「id」=>「1」}
訂單加載(0.1ms)SELECT「orders」。* FROM「orders」WHERE「orders」。「id」=? LIMIT 1 [[ 「ID」,1]]
渲染命令/佈局內show.html.erb /應用(1.9ms)
完成200 OK中50ms的(查看:47.8ms | ActiveRecord的:爲0.1ms)

如何使用適當的狀態字段更新來更新模型。 'status'是'orders'模型中的一個整數字段。

+0

我無法弄清楚你想要什麼。真。你能否詳細解釋一下?但是,您可以將動作傳遞給link_to函數,如[question](http://stackoverflow.com/questions/5607155/ruby-on-rails-3-link-to-controller-and-action) – 2014-11-21 23:58:55

+0

中所述是的,你指出的答案看起來像在這種情況下會起作用。 link_to僅以id或整個對象作爲輸入參數調用控制器中的操作。 – Atarang 2014-11-23 07:23:00

回答

2

我會做到這一點與AJAX:

您需要在您的的routes.rb文件類似的東西。

resources :orders 

訂單/ show.html.erb:

<div id="order" data-order-id="<%= @order.id %>"></div> 

<p id="notice"><%= notice %></p> 

<p> 
    <strong>Status:</strong> 
    <%= @order.status %> 
</p> 

<p> 
    <strong>Name:</strong> 
    <%= @order.name %> 
</p> 

<%= link_to 'Edit', edit_order_path(@order) %> | 
<%= link_to 'Back', orders_path %> 
<%= link_to 'Shipped', '',id: "ship" %> | 
<%= link_to 'Close', '', id: "close" %> 



<script> 
$(function() { 
    var orderId = $('#order').data("order-id"); 
    var shippButton = $('#ship'); 

    var closeButton = $('#close'); 
    alert(orderId); 



    closeButton.on("click", function() { 
     $.ajax({ 
      url: '/orders/'+orderId, 
      type: 'PATCH', 
      dataType: 'json', 
      data: {"order": { "status": "4"}}, 
      complete: function (jqXHR, textStatus) { 
       // callback 
      }, 
      success: function (data, textStatus, jqXHR) { 
       // inform user that it was a sucess like: 
       alert("successfully changed the order to closed"); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       // do something if the request fails 
      } 
     }); 
    }); 

    shippButton.on("click", function() { 
     $.ajax({ 
      url: '/orders/'+orderId, 
      type: 'PATCH', 
      dataType: 'json', 
      data: {"order": { "status": "3"}}, 
      complete: function (jqXHR, textStatus) { 
       // callback 
      }, 
      success: function (data, textStatus, jqXHR) { 
       // inform user that it was a sucess like: 
       alert("successfully changed the order to shipped"); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       // do something if the request fails 
      } 
     }); 
    }); 
}); 

</script> 
+0

@ auL5agoi,謝謝,我會嘗試更改並更新我的回覆。但是因爲我從來沒有做過任何Javascript,我會把這些JS函數放在show.html.erb中?他們是否在某種類型的HTML標籤? – Atarang 2014-11-22 00:53:09

+0

@Atarang你好,你可以將它們包裝在一個腳本標記中,這些都在你的show.html.erb文件中。更新了我的答案。順便說一句。請描述您需要更改哪些屬性:例如:狀態:1時x,狀態:2當y等。 – 2014-11-22 01:02:39

+0

@ auL5agoi,我還沒有嘗試過你的建議。我已訂購模型,其中有狀態哈希與{:新=> 1,:開放=> 2,:發運=> 3,:關閉=> 4}並取決於點擊'運送'或'關閉'我需要將適當的值設置爲模型中的「狀態」字段。爲什麼你的方法被認爲是'壞'? Rails在沒有JS代碼的情況下實現同樣的事情的方式是什麼? – Atarang 2014-11-22 08:20:45