Je construis un formulaire pour une relation :has_many :through
:NoMethodError avec collection_select
class Account < ActiveRecord::Base
has_many :employments
has_many :people, :through => :employments
accepts_nested_attributes_for :employments
end
class Person < ActiveRecord::Base
has_many :employments
has_many :accounts, :through => :employments
end
class Employment < ActiveRecord::Base
belongs_to :account
belongs_to :person
end
Le modèle Employment
contient les champs :account_id
et :person_id
.
Dans le formulaire de compte, je suis d'ajouter:
<% fields_for 'account[employments_attributes][]', @account.employments do |e| %>
<%= e.hidden_field :account_id, :value => @account.id %>
<%= e.collection_select :person_id, Person.all, :id, :name %>
<% end %>
collection_select
, ou select
ainsi, dans toute permutation je leur donne échouent avec une exception NoMethodError:
undefined method `person_id' for #<Array:0x82e7db0>
Il est comme si le champ person_id
n'existe pas et pourtant je peux appeler la méthode create avec :account_id
et :person_id
parfaitement bien.