I am new at ruby and rails and on my studies I’ve created this Payment Types array of my Order..
class Order < ActiveRecord::Base
PAYMENT_TYPES = [{:id => "check", :name => "Check"}, {:id => "credit_card", :name => "Credit card"}, {:id => "purchase_order", :name => "Purchase order"}]
and I would like to use this way
<%= f.select :pay_type, Order::PAYMENT_TYPES.each {|order| [order.name, order.id]}, :prompt => 'Select a payment method' %>
but I’m getting this error
undefined method `name' for {:id=>"check", :name=>"Check"}:Hash
what is wrong?
In your
eachblock, the variableorderis being filled each time with a hash. Therefore you need to access it like a hash withorder[:name]andorder[:id]instead of.nameand.id.Edit
Also, for maintainability, you may want to create a separate PaymentType model. This will give you much more power and flexibility. With your current method, what happens if you want to add or remove a payment type later? You’ll have to edit the source code. What if you only want certain payment methods to be available for certain transactions?