I'm not very experienced with ROR, is it a clean way to create shipping options in a rails application ?
Just added this code in my orders_form :
<% if @order.subtotal >= 30 %>
(free shipping)
<%= f.radio_button(:shipping, "0.00", :checked => "checked", class: "hidden_field") %><br/>
<% else %>
<p><strong>Shipping options :</strong></p>
<%= f.label 'economy' %>
<%= f.radio_button(:shipping, "3.00", :checked => "checked") %><br/>
<%= f.label 'fast' %>
<%= f.radio_button(:shipping, "5.00") %>
<% end %>
it seems too simple to me to be correct.
Everything's fine, but it's better to keep all this logic inside models, not in views. Something like this:
# order.rb
class Order < ActiveRecord::Base
FREE_SHIPPING_THRESHOLD = 30.0
ECONOMY_SHIPPING_PRICE = 3.0
FAST_SHIPPING_PRICE = 5.0
...
def eligible_for_free_shipping?
subtotal >= FREE_SHIPPING_THRESHOLD
end
end
Then <% if @order.subtotal >= 30 %>
becomes <% if @order.eligible_for_free_shipping? %>
and instead of hardcoding shipping prices in views you use Order::ECONOMY_SHIPPING_PRICE
and Order::FAST_SHIPPING_PRICE
.
With this approach you will have all order shipping settings in one place, so you don't need to go through many-many files when you want to change something.