Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add extra data to a simple_form input

I'm using the simple_form gem.

I'm rendering an input based on a collection (a list of all my actiontypes)

<%= f.association :actiontype, collection: Actiontype.all, input_html: { data: {'impacts-pnl' => ??}} %>

I would like to be able to add a data-attribute to the input to store extra data.

In this case, I want to store the impacts_pnlattribute of my actiontype. The only problem is that I don't know how to refer to the current actiontype

collection.impacts_pnl doesn't work (obviously)

actiontype.impacts_pnlneither.

how can I pass this extra bit of data to my input?

like image 411
Pierre Avatar asked Jul 09 '12 15:07

Pierre


1 Answers

If you want to add these attributes to the option-elements of a selectfield, you should alter the collection by using for example the .map() function. Also, use the input helper with block to do this, otherwise it doesn't work;

= f.input :actiontype do
    = f.select :actiontype, Actiontype.all.map{|a| [a.name, a.id, {"data-impacts-pnl" => p.impacts_pnl}]}

For more information about this issue, see https://github.com/plataformatec/simple_form/issues/188

like image 200
Martijn de Kuijper Avatar answered Nov 27 '22 04:11

Martijn de Kuijper