gpt4 book ai didi

ruby-on-rails - Ruby On Rails : How do I create multple text_field_tags that store the values into the same array?

转载 作者:行者123 更新时间:2023-12-03 15:46:03 24 4
gpt4 key购买 nike

我需要能够创建一个表单,其中有几个 text_field_tags 根据另一个模型的实例动态创建。我希望能够将输入到这些 text_field_tags 的值存储到同一个数组中,一旦提交表单,我就可以作为 Controller 中的参数访问该数组。这可能吗?

最佳答案

如果您给它们都赋予相同的名称并在末尾附加 [],如下所示:

 <%= text_field_tag "some_fields[]" %>
<%= text_field_tag "some_fields[]" %>
<%= text_field_tag "some_fields[]" %>

您可以从 Controller 访问这些:
  some_fields = params[:some_fields] # this is an array

如果您在方括号之间输入值,rails 会将其视为散列:
 <%= text_field_tag "some_fields[1]" %>
<%= text_field_tag "some_fields[2]" %>
<%= text_field_tag "some_fields[3]" %>

将被 Controller 解释为带有键“1”、“2”和“3”的散列

关于ruby-on-rails - Ruby On Rails : How do I create multple text_field_tags that store the values into the same array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12188436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com