多选字段用于一个属性 Rails 7

huangapple go评论108阅读模式
英文:

multiple select fields for one attribute rails 7

问题

我有多个具有相同属性的选择字段,但属性只能检测到第一个选择字段,这当然不是我想要的。

视图:

```html
<div class="dispo">
  <div class="di-in">
    <%= form.select :disponibilities, [["6", "6"], ["7", "7"], ["8", "8"],  ["9", "9"],  ["10", "10"],  ["11", "11"],  ["12", "12"],  ["13", "13"],  ["14", "14"],  ["15", "15"],  ["16", "16"],  ["17", "17"],  ["18", "18"],  ["19", "19"],  ["20", "20"]] %> 
    -
    <%= form.select :disponibilities, [["6", "6"], ["7", "7"], ["8", "8"],  ["9", "9"],  ["10", "10"],  ["11", "11"],  ["12", "12"],  ["13", "13"],  ["14", "14"],  ["15", "15"],  ["16", "16"],  ["17", "17"],  ["18", "18"],  ["19", "19"],  ["20", "20"]] %> 
  </div>
  <div class="di-in">
    <%= form.select :disponibilities, [["6", "6"], ["7", "7"], ["8", "8"],  ["9", "9"],  ["10", "10"],  ["11", "11"],  ["12", "12"],  ["13", "13"],  ["14", "14"],  ["15", "15"],  ["16", "16"],  ["17", "17"],  ["18", "18"],  ["19", "19"],  ["20", "20"]] %> 
    -
    <%= form.select :disponibilities, [["6", "6"], ["7", "7"], ["8", "8"],  ["9", "9"],  ["10", "10"],  ["11", "11"],  ["12", "12"],  ["13", "13"],  ["14", "14"],  ["15", "15"],  ["16", "16"],  ["17", "17"],  ["18", "18"],  ["19", "19"],  ["20", "20"]] %> 
  </div>
  ...
</div>

我的information_trainer.rb:

```ruby
class InformationTrainer < ApplicationRecord
    belongs_to :account
    has_many :disponibilitiess
end

我添加了has_many关联,但在控制台中提交表单时仍然会渲染出“6”。我希望它能记住每个选择的数字。有人知道如何修复这个问题吗?


<details>
<summary>英文:</summary>
I have multiple select fields with the same attribute on each select field, though the attribute only detects the first select field which of course i don&#39;t want.
the view :
```html
&lt;div class=&quot;dispo&quot;&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;div class=&quot;di-in&quot;&gt;
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
-
&lt;%= form.select :disponibilities, [[&quot;6&quot;, &quot;6&quot;], [&quot;7&quot;, &quot;7&quot;], [&quot;8&quot;, &quot;8&quot;],  [&quot;9&quot;, &quot;9&quot;],  [&quot;10&quot;, &quot;10&quot;],  [&quot;11&quot;, &quot;11&quot;],  [&quot;12&quot;, &quot;12&quot;],  [&quot;13&quot;, &quot;13&quot;],  [&quot;14&quot;, &quot;14&quot;],  [&quot;15&quot;, &quot;15&quot;],  [&quot;16&quot;, &quot;16&quot;],  [&quot;17&quot;, &quot;17&quot;],  [&quot;18&quot;, &quot;18&quot;],  [&quot;19&quot;, &quot;19&quot;],  [&quot;20&quot;, &quot;20&quot;]] %&gt; 
&lt;/div&gt;
&lt;/div&gt;

my information_trainer.rb

class InformationTrainer &lt; ApplicationRecord
belongs_to :account
has_many :disponibilitiess
end

I added the has_many association but still it renders me "6" in the console when I submit the form, I want it to memorise every selected number. Anyone has an idea how to fix this ?

答案1

得分: 1

如果您需要 params => {&quot;disponibilities&quot;=&gt;[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]},那么您的选择标签 name 需要以 [] 结尾,这在选择标签上感觉有点奇怪:

&lt;%= form_with url: &quot;/&quot; do |f| %&gt;
  &lt;%= f.fields_for &quot;disponibilities&quot; do |ff| %&gt;
    &lt;%= ff.select &quot;&quot;, 1..10 %&gt;
    &lt;%= ff.select &quot;&quot;, 1..10 %&gt;
  &lt;% end %&gt;

  &lt;%= select_tag &quot;disponibilities[]&quot;, options_for_select(1..10) %&gt;
  &lt;%= select_tag &quot;disponibilities[]&quot;, options_for_select(1..10) %&gt;

  &lt;%= f.submit %&gt;
&lt;% end %&gt;
参数:{&quot;disponibilities&quot;=&gt;[&quot;4&quot;, &quot;3&quot;, &quot;10&quot;, &quot;9&quot;], &quot;commit&quot;=&gt;&quot;Save &quot;}

也许您想要复选框或实际的多选框。如果您尝试更改关联关系并且数字是 ID,请使用 disponibility_ids,但如果存在重复选择,则不会起作用,您可能需要拥有多对多关系(类似于此 https://stackoverflow.com/a/75972917/207090):

&lt;%= f.collection_check_boxes &quot;disponibility_ids&quot;, 1..10, :to_s, :to_s %&gt;

&lt;%= f.select &quot;disponibility_ids&quot;, 1..10, {}, multiple: true %&gt;

https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html

英文:

If you need params => {&quot;disponibilities&quot;=&gt;[&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]} then your select tag names need [] at the end, which feels weird on a select tag:

&lt;%= form_with url: &quot;/&quot; do |f| %&gt;
  &lt;%= f.fields_for &quot;disponibilities&quot; do |ff| %&gt;
    &lt;%= ff.select &quot;&quot;, 1..10 %&gt;
    &lt;%= ff.select &quot;&quot;, 1..10 %&gt;
  &lt;% end %&gt;

  &lt;%= select_tag &quot;disponibilities[]&quot;, options_for_select(1..10) %&gt;
  &lt;%= select_tag &quot;disponibilities[]&quot;, options_for_select(1..10) %&gt;

  &lt;%= f.submit %&gt;
&lt;% end %&gt;
Parameters: {&quot;disponibilities&quot;=&gt;[&quot;4&quot;, &quot;3&quot;, &quot;10&quot;, &quot;9&quot;], &quot;commit&quot;=&gt;&quot;Save &quot;}

Maybe you want checkboxes or an actual multiple select box. If you're trying to change associations and the numbers are ids use disponibility_ids, but it won't work if there are duplicate selections, and you probably need to have a many-to-many relationship (like this https://stackoverflow.com/a/75972917/207090)

&lt;%= f.collection_check_boxes &quot;disponibility_ids&quot;, 1..10, :to_s, :to_s %&gt;

&lt;%= f.select &quot;disponibility_ids&quot;, 1..10, {}, multiple: true %&gt;

https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html

huangapple
  • 本文由 发表于 2023年8月11日 04:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定