0

私はこれらの2つのモデルを持っています:

class Person < ActiveRecord::Base
  attr_accessible :contact, :name

  validates :name, :presence => true

  has_many :books

end

class Register < ActiveRecord::Base
  attr_accessible :checkin, :checkout, :notes, :person, :book

end

registers / _form.html.erbで、リストから個人の名前を選択できるf.collection_selectを使用したいと思います。登録モデルの目的は、チェックアウトの履歴を記録することです。

collection_selectを使用するのはこれが初めてであり、stackoverflowとgoogleで読んだ例で頭を悩ませることはできません。たぶん私はモデルに必要なすべてを持っていませんか?

助けてください。

4

1 に答える 1

0

アソシエーションに関してモデルにエラーがあります。正しい関連付けは次のとおりです。

class Person < ActiveRecord::Base
   has_many :registers, :inverse_of => :person
end

class Register < ActiveRecord::Base
   belongs_to :person, :inverse_of => :registers
end

次にregisters/_form.html.erb、次のようなものが必要です。

<%= form_for :register do |f| %>
   <% # ... input elements for the rest of the Register model %>

   <%= f.collection_select :person_id, Person.all, :id, :name %>

   <% # ... input elements for the rest of the Register model %>

   <%= f.submit %>
<% end %>
于 2012-12-04T13:34:59.280 に答える