英文:
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't want.
the view :
```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 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 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 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>
my information_trainer.rb
class InformationTrainer < 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
=> {"disponibilities"=>["1", "2", "3", "4"]}
,那么您的选择标签 name
需要以 []
结尾,这在选择标签上感觉有点奇怪:
<%= form_with url: "/" do |f| %>
<%= f.fields_for "disponibilities" do |ff| %>
<%= ff.select "", 1..10 %>
<%= ff.select "", 1..10 %>
<% end %>
<%= select_tag "disponibilities[]", options_for_select(1..10) %>
<%= select_tag "disponibilities[]", options_for_select(1..10) %>
<%= f.submit %>
<% end %>
参数:{"disponibilities"=>["4", "3", "10", "9"], "commit"=>"Save "}
也许您想要复选框或实际的多选框。如果您尝试更改关联关系并且数字是 ID,请使用 disponibility_ids
,但如果存在重复选择,则不会起作用,您可能需要拥有多对多关系(类似于此 https://stackoverflow.com/a/75972917/207090):
<%= f.collection_check_boxes "disponibility_ids", 1..10, :to_s, :to_s %>
<%= f.select "disponibility_ids", 1..10, {}, multiple: true %>
https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html
英文:
If you need params
=> {"disponibilities"=>["1", "2", "3", "4"]}
then your select tag name
s need []
at the end, which feels weird on a select tag:
<%= form_with url: "/" do |f| %>
<%= f.fields_for "disponibilities" do |ff| %>
<%= ff.select "", 1..10 %>
<%= ff.select "", 1..10 %>
<% end %>
<%= select_tag "disponibilities[]", options_for_select(1..10) %>
<%= select_tag "disponibilities[]", options_for_select(1..10) %>
<%= f.submit %>
<% end %>
Parameters: {"disponibilities"=>["4", "3", "10", "9"], "commit"=>"Save "}
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)
<%= f.collection_check_boxes "disponibility_ids", 1..10, :to_s, :to_s %>
<%= f.select "disponibility_ids", 1..10, {}, multiple: true %>
https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论