24

Although the code seems to be right, when I try to send the form, the values of the multiple select aren't being sent.

If I just remove the multiple option, everything works as expected considering just one value, but it's important to store more than one tag per transaction.

Model

Transaction.rb

class Transaction < ActiveRecord::Base
    has_and_belongs_to_many :tags

Tag.rb

class tag < ActiveRecord::Base
    has_and_belongs_to_many :transactions

View

<%= form.collection_select :tag_ids, @tags, :id, :name, {}, 
    {:multiple => true} %>

Result:

<select id="transaction_tag_ids" multiple="multiple" name="transaction[tag_ids][]">  
    <option value="1">..</option>
</select>
4

2 回答 2

43

确保您正确允许接收到的参数用于批量分配。

你说一个参数有效,所以我假设你在控制器的某个地方有什么,比如:

params.require(:transaction).permit(:name, :tag_ids)

所以你需要允许接收一个数组:

params.require(:transaction).permit(:name, :tag_ids => [])
于 2013-07-30T14:27:30.800 回答
0

我解决了我的问题

{:health_unit_ids => []}

没有{},Rails 4 不被接受

于 2016-04-18T13:55:52.677 回答