Robert Ross
Robert Ross

Reputation: 1925

Formtastic array inputs

I have a a serializable field on my rails model that allows arrays.

Question is, in formtastic, how would I get the output:

<input type="text" name="model[variables][]" />

I'll tell it the value, im not worried about that.

Upvotes: 2

Views: 3292

Answers (1)

Grimmo
Grimmo

Reputation: 1505

As i know, formtastic can't generate text inputs for array values. And this is not a formtastic issue.

How would the list of text values look like(labels, validation errors etc.)? As a list of checkboxes? - then use

f.input :authors, :as => :check_boxes, 
        :collection => current_user.company.users.active

Your concern is probably the name of each input because it reflects to request 'params'. It is possible to implement custom formtastic field type or simply use Rails helpers to achieve what you need.

E.g. pass array as a hidden value:

= semantic_form_for @model do |form|
  - @model.variables.each do |value|
    = hidden_field_tag 'model[variable][]', value, id: "model_variable_#{value}"

Then you will have params[:model][:variables] to be an array.

Upvotes: 4

Related Questions