0

連絡先を作成し、グループを作成するための CRUD を用意しています。どちらもユーザー モデルの下にネストされています。

連絡先をグループに関連付ける方法を知る必要があります。

ユーザーが連絡先が属するグループを選択できるように、連絡先フォームに(formtasticを使用して)いくつかのチェックボックスを設定したいと思います。

PHP では、contacts_to_groups というテーブルを作成し、contact_id と group_id の列を作成します。連絡先を保存するときに、そのデータを渡し、結合を使用して後で元に戻します。

ありがとう!

お問い合わせ作成フォーム

<%= semantic_form_for [@contact.user, @contact] do |f| %>
<% f.inputs do %>
    <%= f.input :firstname, :label => 'First Name' %>
    <%= f.input :lastname, :label => 'Last Name' %>
    <%= f.input :email, :label => 'Email' %>

    <%= f.input :notes, :input_html => { :class => 'autogrow', :rows => 10, :cols => 50, :maxlength => 10  }, :label => 'Notes' %>
<% end %>


<%= f.buttons %>

<%終了%>

4

1 に答える 1

0

モデルを次のように修正します。

class Group < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :contacts  
end

class Contact < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :groups
end

そして、DB contact_groups(contact_id, group_id) にテーブルを作成する必要があります

于 2011-05-26T14:34:29.753 に答える