Ace Dimasuhid
Ace Dimasuhid

Reputation: 1062

Simple form: how to set values of a collection

I have the following code below:

= simple_form_for :credits, url: "/accounts/#{@account.id}/topup" do |f|
  = f.input :amount, collection: [100,500,1000,5000,10000],as: :radio_buttons
  = f.button :submit

It works to set all values and labels in the collection. What I want is something like,

label: 100, value: 500

How would that happen?

Upvotes: 2

Views: 5142

Answers (3)

dax
dax

Reputation: 10997

Something like this (from the simpleform github).

Their example is:

form_for @user do |f|
  f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
end

and

so i think yours should looks something like:

= simple_form_for :credits, url: "/accounts/#{@account.id}/topup" do |f|
  = f.collection_check_boxes :amount, [[100, 500], [500, 'a'], [1000, 'b'], [5000, 'c'], [10000, 'd']]
= f.button :submit

Upvotes: 3

Manoj Sehrawat
Manoj Sehrawat

Reputation: 1303

Try this, worked for me:

= simple_form_for :credits, url: "/accounts/#{@account.id}/topup" do |f|
  = f.input :amount, collection: [['100','500'], ['1000','5000'] ,['10000', '23']], as: :radio_buttons
  = f.button :submit

Upvotes: 0

Matt
Matt

Reputation: 14038

You need to use an array of arrays:

[[100, "One Hundred"], [200, "Two Hundred"], [300, "SPARTA!"]] 

You can then set the value and label separately.

Upvotes: 0

Related Questions