ids in the form
i have a event model that has_and_belongs_to_many artists
开发者_C百科class Event < ActiveRecord::Base
has_and_belongs_to_many :humans, :foreign_key => 'event_id', :association_foreign_key => 'human_id'
end
in the form for the event inserting, i put an hidden field for the artists ids:
<%= event_form.text_field :artist_ids %>
If I inserted manually the value "8,9,10" (ids associated to 2 rows of humans) and I submit the form, in the controller I obtain only 8.
Why?
How can I do?
When you assign the string "8,9,10"
to artist_ids
it gets converted to an integer value:
>> a.artist_ids = '1,2,3'
=> "1,2,3"
>> a.artist_ids
=> [1]
You need to split it before you pass it to the model:
>> a.artist_ids = '1,2,3'.split(',')
=> ["1", "2", "3"]
>> a.artist_ids
=> [1, 2, 3]
In your "new" action how do you find the artist_ids? You will need to use the same query in the action that the form submits to. (So if new submits to create, you need to find the artists in the create action).
精彩评论