简体   繁体   English

Rails和Postgres数组列

[英]Rails and Postgres array columns

I have a model with an array column (box_ids). 我有一个带有数组列(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. 我不知道这是否是正确的方法,但是无论如何我都无法提供给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. 但这需要控制器中的特殊处理-如果可能的话,我想避免这种情况。

  <%= 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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM