我在我的應用程序中有一個頁面,用戶在其中選擇一個運輸選項。我需要將他們的選擇添加到數據庫中的order.total
。我在order.rb
模型下面的自定義方法:用simple_form在rails中調用提交模型方法
def update_order_from_shipping_page(shipping)
new_total = self.total + self.shipping
self.update_attributes(total: new_total)
end
在我看來,以下形式:
%= simple_form_for @order, url: charges_update_order_path(:shipping), method: :post do |f| %>
<div class="row">
<div class="form-inputs text-left">
<div class="form-group col-sm-6">
<%= f.collection_radio_buttons :shipping, shipping_choices, :first, :last, item_wrapper_class: :block_radio_button_collection %>
</div>
</div> <!-- form inputs -->
</div> <!-- choices row -->
<div class="row">
<%= f.button :submit, "Calculate Shipping" %>
</div>
<% end %>
我創建了以下路線:
post 'charges/update_order'
我有這在我的charges_controller
:
def update_order
@order = current_order
if @order.update_order_from_shipping_page(shipping)
redirect_to new_charge_path and return
else
redirect_to :back
flash[:notice] = "Something is amuck."
end
end
正確填充單選按鈕並且不顯示控制檯或服務器錯誤,但charges#new
頁面上顯示的總數不反映模型方法會觸發的更新。任何人都可以看到我要去哪裏嗎?
創建一個操作並提交給它。 http://stackoverflow.com/a/7507925/4643970 –
@MahmoudSayed,謝謝你的參考!我更新了OP,但仍然沒有看到模型方法生效。 – Liz