Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a model with an array column (box_ids). In my view I would like to have a field for each of the values in the array and three extra empty fields to be able to add new values to the array. How to go about this? I have the following:

  <%= f.fields_for "box_ids[]", @shop do |bid| %>
    <div class="form-group">
      <%= bid.label :box_id %> Box ID: <%= bid.text_field [WHAT HERE] %>
    </div>
  <% end %>

I don't know if this is the right approach but in any case I have no method to supply to text_field.

Any suggestions?

Edit:

This works:

  <% @shop.box_ids.each  do |bid| %>
    <div class="form-group">
      <%= label_tag :box_id %> Box ID: <%= text_field_tag "box_ids[]", bid %>
    </div>
  <% end %>
  <% 3.times do %>
    <div class="form-group">
      <%= label_tag :box_id %> Box ID: <%= text_field_tag "box_ids[]" %>
    </div>
  <% end %>

But that requires special handling in the controller - I would like to avoid that if possible.

share|improve this question
    
can you inspect bid? –  emaillenin Jun 18 at 9:38
    
Yes - it is an ActionView::Helpers::FormBuilder –  jriff Jun 18 at 9:40
    
And bid.object is a Shop class. –  jriff Jun 18 at 9:41

1 Answer 1

  <%= f.fields_for "box_ids[]", @shop do |bid| %>
    <div class="form-group">
      <%= bid.label :box_id %> Box ID: <%= bid.text_field :box_ids %>
    </div>
  <% end %>
share|improve this answer
    
Thanks for your suggestion. But that just gives: undefined method `1' for #<Shop:0x007faf763f3720> –  jriff Jun 18 at 9:48
    
try box_ids directly –  emaillenin Jun 18 at 10:04
    
That just gives me {1,2,3} in the text field. –  jriff Jun 18 at 10:07
    
try updating that single textbox value to {4,5,6} and save. check in DB if it works. –  emaillenin Jun 18 at 10:08
    
It works. BTW. see my edit to the OQ. –  jriff Jun 18 at 10:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.