foggedin
foggedin

Reputation: 11

What is the best way to create a form for editing a model attribute which is an array of strings in Rails?

Take a model with an attribute that is an array of strings. This attribute is serialized to the database.

class MyModel < ActiveRecord::Base
  serialize :str_array
end

In the edit view I would like to display a text field for each element of the array allowing the user to view and modify the attributes elements.

Upvotes: 1

Views: 176

Answers (1)

Alan Peabody
Alan Peabody

Reputation: 3557

Not up on my haml syntax, but this should get you going:

# form

<% form_for :my_model do |f| %>

<% render :partial => 'str_field', :collection => @my_model.str_array %>

<% end %>

# _str_field partial:

<input type="text" value="<%=str_field-%>" name="my_model[str_array][]" />

There is also a str_field_counter variable that may be of use for you.

You can definitely customize to your basic situation, but that is the basics.

Upvotes: 1

Related Questions